:io.format error with X in control sequence

In the erlang shell I can run

io:format("~32.8X~n", [8, "0o"]).
                            0o10

and the output is as expected 0o10. But when I try to run the same thing in iex as

:io.format("~32.8X~n", [8, "0o"])
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: failed to format string

    (stdlib 3.17) io.erl:99: :io.format("~32.8X~n", [8, "0o"])

I cannot for the life of me figure out what the difference is. I’m literally copying and pasting the arguments so if it works in the erlang shell version it ought to work in the iex version. Any clues?

Erlang’s string type (charlist) is used with single quotes in Elixir. The second argument has to receive an Erlang string, so try:

:io.format("~32.8X~n", [8, '0o'])

Elixir’s strings are UTF-8 encoded binaries (which work as “strings” in some Erlang functions but not all).

This page lists some other differences between Elixir and Erlang: Erlang/Elixir Syntax: A Crash Course - The Elixir programming language

2 Likes

So simple but I would never have figured it out on my own. Thanks for your help!