z1naOK9nu8iY5A
z1naOK9nu8iY5A

Reputation: 893

Printing integer in hexadecimal with Elixir

I can do this in Erlang:

io:fwrite("~.16X~n", [-31,"0x"]).
-0x1F
ok

but not in Elixir:

:io.fwrite("~.16X~n", [-31,"0x"]) ** (ArgumentError) argument error (stdlib) :io.format(#PID<0.54.0>, "~.16X~n", [-31, "0x"])

Why not?

Upvotes: 3

Views: 1457

Answers (2)

Grych
Grych

Reputation: 2901

Why not to use Integer.to_string/2?

iex(1)> Integer.to_string(-31, 16)
"-1F"

Upvotes: 7

Onorio Catenacci
Onorio Catenacci

Reputation: 15293

Adding @Dogbert's comment as an answer:

Try using single quotes:

:io.fwrite('~.16X~n', [-31, '0x'])

A word of additional explanation: single quotes in Elixir indicate a character list (see here for more details). The Erlang fwrite function is expecting a list of characters not an Elixir binary hence the double quotes don't work while the single quotes do.

Upvotes: 3

Related Questions