I have 4 bytes of binary data (big-endian) that I want to unpack. If it contained two 2-byte unsigned integer values, this would be straightforward:
a, b = data.unpack("C>C>")
But what if the data contains a 3-byte value (a
) followed by a 1-byte value (b
)? The unpack method doesn't seem to be able to handle formats other than 8-, 16-, 32-, and 64-bit integers. This is what I came up with:
a, b = data.unpack("L>XC") # "L>": unpack a 32-bit unsigned int (big-endian)
# "X": rewind (skip back) one byte
# "C": unpack an 8-bit unsigned int
a >>= 8 # drop the last (lowest) byte from a
(If the data were little-endian, a &= 0xFFFFFF
could be used to drop the last (highest) byte instead.)
Is there a more elegant way to unpack these values?