From: Harris Reynolds Date: 2006-06-09T11:39:09+09:00 Subject: Re: Converting 8 bytes to a Float --0-750032737-1149820745=:30076 Content-Type: text/plain; charset=us-ascii Mike... thanks for the tip. My mileage is varying though... here is the same irb session on my machine: irb(main):004:0> s = "" => "" irb(main):005:0> s << 64 << 53 << "\0" * 6 => "@5\000\000\000\000\000\000" irb(main):006:0> s.unpack('D') => [6.73510288410787e-320] I'm not getting the clean result for some reason. ~harris ----- Original Message ---- From: Mike Stok To: ruby-talk ML Sent: Thursday, June 8, 2006 8:57:06 PM 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. --0-750032737-1149820745=:30076--