Bitwise operations

R

Rob Lee

Hi,

I have a binary packet stored in a string. I also have a packet
specification and I'd like to print the first byte as a hex value to
check it corresponds to the expected value from the specification
('7F'). I can't understand how to access the first byte from a string
(a series of 8 bit bytes I believe). There don't seem to be any
bit-shift operators in Ruby for a string - could anybody advise how I go
about this ?

Thanks
 
J

Joel VanderWerf

Rob said:
Hi,

I have a binary packet stored in a string. I also have a packet
specification and I'd like to print the first byte as a hex value to
check it corresponds to the expected value from the specification
('7F'). I can't understand how to access the first byte from a string
(a series of 8 bit bytes I believe). There don't seem to be any
bit-shift operators in Ruby for a string - could anybody advise how I go
about this ?

It would be overkill for just getting the first byte, but you may find
bit-struct helpful in defining and parsing binary strings.

http://raa.ruby-lang.org/project/bit-struct/
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top