How to convert integers to binaries in elixir?

How can I convert integer like 1 to binary thanks.

Kernel.to_string/1 or Integer.to_string/2

Ok, lets try again. I accidentally deleted the other post.

I usually convert integer to its binary representation like this:

binval = <<intval::integer-size(32)>>

The various options can be found here: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#<<>>/1

and are used to decide things like size, endianess and signedness.

2 Likes

is something like I want to convert integer into binary figures for example:

1 -> 101010

then I will use 101010 for another process.

Integer.to_string accepts a base parameter…

iex> Integer.to_string(150, 2)
"10010110"
6 Likes