How do two objects communicate?

A

Austin Ziegler

I did - but could see no way to pack a binary string.
Is there a way to pack a binary string of bytes?

Can you post an example of what it is that you're trying to do?

-austin
 
A

ara.t.howard

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
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top