fireproofsocks

fireproofsocks

Why is the output of single-quoted strings sometimes numbers, sometimes just the string?

Can someone explain this behavior?

iex> 'hełło'
[104, 101, 322, 322, 111]
iex> 'hello'
'hello'

Why isn’t there a consistent output? If single-quoted strings are character lists, why are the code points only revealed when the string contains multi-byte characters? How can I see the internal integer codepoints of any single-quoted string?

Strings/charlists are confusing enough without the obfuscation. Could someone shed light on this?

Marked As Solved

fireproofsocks

fireproofsocks

The <<0>> null-byte trick is good, but I think you have to adapt its syntax when working with character lists:

iex> 'hello' ++ [0]
[104, 101, 108, 108, 111, 0]

The charlists: :as_lists is the most helpful option here:

iex> IO.inspect('hello', charlists: :as_lists)
[104, 101, 108, 108, 111]

It’s the counterpart to the binaries: :as_binaries when working with binaries:

iex(10)> IO.inspect("hello", binaries: :as_binaries)
<<104, 101, 108, 108, 111>>
"hello"

Also Liked

LostKobrakai

LostKobrakai

In particular, charlists will be printed back by default in single quotes if they contain only printable ASCII characters

https://hexdocs.pm/elixir/List.html#module-charlists

rvirding

rvirding

Creator of Erlang

The reason for sometimes printing lists of integers as ' ' strings is that this is how strings are represented in Erlang, as lists of unicode codepoints. And the reason for this is that there is no string datatype in the BEAM, no character datatype either for that matter. So we fake strings.

In Erlang with lists of integers, which is a “classic” way of doing it in functional languages, and in Elixir with binaries containing UTF-8 encode chararcters.

iex(8)> "abc"         
"abc"
iex(9)> "abc" <> <<0>>
<<97, 98, 99, 0>>
iex(10)> "a™b"
"a™b"
iex(11)> "a™b" <> <<0>>
<<97, 226, 132, 162, 98, 0>>

using the append a 0 trick to show the internals.

It’s a hard life. :wink:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement