Elixir Language Char Lists


Strings in Elixir are "binaries". However, in Erlang code, strings are traditionally "char lists", so when calling Erlang functions, you may have to use char lists instead of regular Elixir strings.

While regular strings are written using double quotes ", char lists are written using single quotes ':

string = "Hello!"
char_list = 'Hello!'

Char lists are simply lists of integers representing the code points of each character.

'hello' = [104, 101, 108, 108, 111]

A string can be converted to a char list with to_charlist/1:

iex> to_charlist("hello")

And the reverse can be done with to_string/1:

iex> to_string('hello')

Calling an Erlang function and converting the output to a regular Elixir string:

iex> :os.getenv |> hd |> to_string