From: Mike Stok Date: 2006-06-09T09:57:06+09:00 Subject: Re: Converting 8 bytes to a Float On 8-Jun-06, at 8:47 PM, Harris Reynolds wrote: > I am still not getting the results I am looking for hacking around > with the couple suggestions I received. Here is an example: > > bytes = Array.new(8) > bytes[0] = 64 > bytes[1] = 53 > bytes[2] = 0 > bytes[3] = 0 > bytes[4] = 0 > bytes[5] = 0 > bytes[6] = 0 > bytes[7] = 0 > > Calling: > > binary_string = bytes.pack('D') > > returns a binary string, but not the float which should be 21.0; if > I turn around and call > > binary_string.unpack('d'), I end up with the original bytes. > > It is turning out that getting from bytes to a Float object is a > bit painful. Is there something easy I am missing? You should be using unpack ;-) . In irb: ratdog:~ mike$ irb irb(main):001:0> s = "" => "" irb(main):002:0> s << 64 << 53 <<"\0" * 6 => "@5\000\000\000\000\000\000" irb(main):003:0> s.unpack('D') => [21.0] Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.