Search code examples
encodinginterlangelixirradix

How to convert a binary to a base10 (decimal) integer in elixir


I would like to be able to convert an elixir string(binary) to a base10 integer.

I have been able to do this with the following...

<<104, 101, 108, 108, 111>> # equal to string "hello"
|> Base.encode16()
|> Integer.parse(16)

{448378203247, ""}

The above does what I am after but, feels a little like a hack. I would like to...

  • better understand what exactly is happening here
  • know if/how I would be able to do this in one step

Solution

  • Since Elixir strings are just binaries, you could probably use the erlang :binary.decode_unsigned function to convert binary digits to integers

    From the documentation http://erlang.org/doc/man/binary.html#decode_unsigned-1

    iex> :binary.decode_unsigned("hello")
    448378203247
    
    iex> :binary.encode_unsigned(448378203247)
    "hello"
    

    Essentially, the ascii values of hello is

    <<104, 101, 108, 108, 111>>
    

    when converted from decimal to hex can be written as

    <<68, 65, 6C, 6C, 6F>>
    

    or in binary as

    <01101000, 01100101, 01101100, 01101100, 01101111>
    

    which is a series of bytes stored as

    68656C6C6F in hex or

    01101000_01100101_01101100_01101100_01101111 in binary

    whose decimal(base-10) value would be 448378203247

    iex> Integer.to_string(448378203247, 16)
    "68656C6C6F"
    
    iex> Integer.to_string(448378203247, 2)
    "110100001100101011011000110110001101111"
    # each byte separated by _ is
    # "1101000_01100101_01101100_01101100_01101111"
    # missing a leading zero at the left, which doesn't change the value
    

    edit: added binary example,

    also, two hex digits can be used to perfectly denote a byte(4 bits needed to encode 16 values, 0 to 15) which is why when we denote in hex, we can just concatenate the hex values and not when they are in decimal(base-10) notation

    From The wiki for hexadecimal

    Hexadecimal numerals are widely used by computer system designers and programmers, as they provide a more human-friendly representation of binary-coded values. Each hexadecimal digit represents four binary digits, also known as a nibble, which is half a byte. For example, a single byte can have values ranging from 0000 0000 to 1111 1111 in binary form, which can be more conveniently represented as 00 to FF in hexadecimal.