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

Popular posts from this blog

qt - QML MouseArea onWheel event not working properly when inside QML Scrollview -

java - is not an enclosing class / new Intent Cannot Resolve Constructor -

python - Error importing VideoFileClip from moviepy : AttributeError: 'PermissionError' object has no attribute 'message' -