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
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.
2 Comments
Anthony Liekens
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?
bandi
@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.