Hex to Float Conversion

400 views
Skip to first unread message

Bryan Joseph

unread,
Mar 6, 2014, 10:32:57 AM3/6/14
to elixir-l...@googlegroups.com
Hi, is there any way in Elixir to turn a hexadecimal into a float?

José Valim

unread,
Mar 6, 2014, 10:40:40 AM3/6/14
to elixir-l...@googlegroups.com
Assuming hex is a binary you can do:

binary_to_integer("ABC", 16) * 1.0



José Valim
Skype: jv.ptec
Founder and Lead Developer


On Thu, Mar 6, 2014 at 4:32 PM, Bryan Joseph <brya...@gmail.com> wrote:
Hi, is there any way in Elixir to turn a hexadecimal into a float?

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bryan Joseph

unread,
Mar 6, 2014, 11:45:10 AM3/6/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Sorry I pressed the reply to author instead of reply to post.

It's a bit more complicated than that. I am creating a geospatial library for Elixir. One of the things I'm trying to do is convert to and from the Well Known Binary format which stores the numbers as floats.

For example: 4000000000000000 should equal 2.0

The example is from wikipedia:

Eric Meadows-Jönsson

unread,
Mar 6, 2014, 12:07:41 PM3/6/14
to elixir-l...@googlegroups.com

First convert the binary to its integer value, like José showed: value = binary_to_integer(binary, 16) then you can convert it to an IEEE754 float with << value :: [float, 64] >>. Change to 32 if it is in binary32 format instead of binary64.

--
Eric Meadows-Jönsson

Bryan Joseph

unread,
Mar 6, 2014, 3:23:47 PM3/6/14
to elixir-l...@googlegroups.com
I'm still trying to get to the correct value. I have the following:

iex(86)> value = binary_to_integer("4000000000000000", 16)
4611686018427387904

iex(87)> new_value = << value :: [float, 64] >>
<<67, 208, 0, 0, 0, 0, 0, 0>>

How do I get from the bitstring to the float value (which in this case should be 2.0)?

José Valim

unread,
Mar 6, 2014, 5:04:03 PM3/6/14
to elixir-l...@googlegroups.com
<< value :: [float, 64] >> = <<67, 208, 0, 0, 0, 0, 0, 0>>

It returns 4.611686018427388e18 though for that particular bitstring.




José Valim
Skype: jv.ptec
Founder and Lead Developer


Bryan Joseph

unread,
Mar 6, 2014, 8:33:23 PM3/6/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
I think this may be something I will have to try to implement myself. The good news is if I get it working I can contribute it back to the language!

Thanks guys. I'm really enjoying Elixir a lot. At this point it has become my language of choice whenever I'm doing something.

Gilbert Kennen

unread,
Mar 6, 2014, 11:17:55 PM3/6/14
to elixir-l...@googlegroups.com
On 03/06/14 17:33, Bryan Joseph wrote:
> I think this may be something I will have to try to implement myself.
> The good news is if I get it working I can contribute it back to the
> language!
>
> Thanks guys. I'm really enjoying Elixir a lot. At this point it has
> become my language of choice whenever I'm doing something.
>

This seems to work.

<< value :: [float, 64] >> = << binary_to_integer("4000000000000000",
16) :: [integer, 64] >>

2.0 = value

It also seems to work with other values such as c000000000000000 and -2.0.

Bryan Joseph

unread,
Mar 7, 2014, 1:12:15 AM3/7/14
to elixir-l...@googlegroups.com, gil...@firewatcher.org
It works! Thanks!
Reply all
Reply to author
Forward
0 new messages