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?
Why not to use Integer.to_string/2?
iex(1)> Integer.to_string(-31, 16)
"-1F"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With