Converting hex floats

What is the easiest way to convert a hex value like:

0x000000000000F03F (big endian, IEEE754)

to a Float in Ruby?


On 3-May-06, at 6:44 PM, Matt Bauer wrote:

Posted via

Probably using pack and unpack, my first guess is something like

irb(main):001:0> [0x000000000000F03F].pack(‘Q’).unpack(‘G’)
=> [3.03865194161742e-319]

but now I have to look up IEEE and it looks reasonable:

irb(main):002:0> [0x0000000000000000].pack(‘Q’).unpack(‘G’)
=> [0.0]
irb(main):003:0> [0x8000000000000000].pack(‘Q’).unpack(‘G’)
=> [-0.0]

and using a sample single precision number in binary from http://

0 10000001 10100000000000000000000 = +1 * 2**(129-127) * 1.101 = 6.5

irb(main):005:0> [0b01000000110100000000000000000000].pack(‘L’).unpack
=> [6.5]

Hope this helps,


Mike S. [email protected]

The “`Stok’ disclaimers” apply.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs