1

I'm using String::unpack to unpack a bunch of ints and double from a binary stream. How can I unpack a single bit from that bitstream? I cannot find a specific directive for decoding single bits with unpack.

1 Answer 1

3

Unpacking can go down to the byte level. You could read an unsigned byte by using the 'C' format and then you can use bitmasks to query individual maps.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I risk going out of bounds when the bit is at the end of the stream and reading 7 more bits using the 'C' directive?
@aliekens I don't think so. By the time the stream gets into to ruby it must consist of bytes. There no such thing as a partial byte. If the data is only 9 bits then it will be represented as 2 bytes and the rest will be padded with zeros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.