Bitwise operations

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

Rob L. wrote:

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs