On Fri, 9 Jun 2006, 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?
>
> thanks,
>
> ~harris

you just need to pack them into a binary string period, since you have the
bytes you don't need to pack them as a float, eg:


   harp:~ > cat a.rb
   f = 21
   buf = [f].pack 'D'
   bytes = buf.unpack 'c*'
   p bytes

   buf = bytes.pack 'c*'
   f = buf.unpack('D').first
   p f


   harp:~ > ruby a.rb
   [0, 0, 0, 0, 0, 0, 53, 64]
   21.0


regards.

-a
-- 
suffering increases your inner strength.  also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama