Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 743
RobStallion Avatar asked Jan 30 '19 13:01

RobStallion


2 Answers

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.

like image 155
S.B Avatar answered Oct 25 '22 23:10

S.B


Convert binary (base 2) number to a decimal (base 10) number in Elixir:

Integer.parse("1111111", 2) |> elem(0) gives 127

:erlang.binary_to_integer(<<"1111111">>, 2) gives 127

While this doesn't answer the OP's example above, it answers his headline.

Also: Elixir gives us shortcuts in its interactive console (IEx) to convert binary, octal and hexadecimal numbers to decimal numbers. Prefix your number with 0b if it's binary, 0o if it's octal or 0x if it's hexidecimal, and it outputs the decimal version.

So 0b1111111 gives 127 in IEx.

Convert decimal to a binary number in Elixir:

Integer.to_string(127, 2) gives "1111111"

like image 27
Raymond Gan Avatar answered Oct 25 '22 21:10

Raymond Gan