Convert a binary string to Hexadecimal and vice-versa in Elixir

Charles Okwuagwu picture Charles Okwuagwu · Oct 26, 2015 · Viewed 8.8k times · Source

How do you convert a binary string to a Hexadecimal String and vice-versa in Elixir?

There are a few posts on SO regarding this topic for other "main stream" languages. There's even an SO post that benchmarks various C# implementations

How do we do this in elixir?

My implementation was too ugly to share... :(

Answer

Gazler picture Gazler · Oct 26, 2015

There is Base.encode16/2:

iex(1)> Base.encode16("foo")
"666F6F"

You can also specify the case:

iex(2)> Base.encode16("foo", case: :lower)
"666f6f"