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