From: ara.t.howard@... Date: 2005-11-29T02:41:08+09:00 Subject: Re: How to read/write an int in binary form to a file? On Mon, 28 Nov 2005, speechexpert wrote: > I did - but could see no way to pack a binary string. Is there a way to pack > a binary string of bytes? harp:~ > irb irb(main):001:0> byte_string = [0,0,0,42].map{|c| c.chr}.join => "\000\000\000*" irb(main):002:0> byte_string.size => 4 irb(main):003:0> byte_string.unpack('N').first => 42 strings are already packed. if you want to treat a string as a collection of bytes do harp:~ > irb irb(main):001:0> byte_string = [0,0,0,42].map{|c| c.chr}.join => "\000\000\000*" irb(main):002:0> bytes = byte_string.split(%r//).map{|b| b[0]} => [0, 0, 0, 42] irb(main):003:0> a = bytes.pack 'c*' => "\000\000\000*" irb(main):004:0> a.unpack('N').first => 42 hopefully you can figure out what to do from the above. kind regards. -a -- =============================================================================== | ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ===============================================================================