ruby - Converting binary string to IEEE 754 float -
can tell me how convert 32-bit binary string ieee 754 floating point value in ruby?
for example, binary string "01000001100101110011001100110011" (0x41973333) should convert 18.9, can't figure out how using ruby.
i'm trying same values returned by: https://www.h-schmidt.net/floatconverter/ieee754.html
thanks.
that fun one!
you need string#unpack , array#pack :
["01000001100101110011001100110011"].pack('b*').unpack('g').first #=> 18.899999618530273 [18.9].pack('g').unpack('b*').first #=> "01000001100101110011001100110011"
'g'
:
g | float | single-precision, network (big-endian) byte order
'b*'
multiple :
b | string | bit string (msb first)
Comments
Post a Comment