bitstring
bitstring copied to clipboard
Support specifying MSB0 or LSB0 when reading ints from a BitStream
I'm decoding packet data which contains (in a particular specified order) arbitrary bit-length integers, some of which are LSB0 and some of which are MSB0. It would be helpful to have the ability to specify the bit ordering when parsing an individual integer out of a stream, such that the bits are read in the order they come in, but interpreted with LSB0 or MSB0 as specified in read. This could mean adding a new format string, such as uintlsb0:n. As an example behavior:
import bitstring
s = bitstring.ConstBitStream('0b00110001')
# 3, 1 as 4 bit ints with LSB last
print(s.readlist(['uint:4', 'uint:4']))
# >>> [3, 1]
s.pos = 0
# 24, 16 as 4 bit ints with LSB first
print(s.readlist(['uintlsb0:4', 'uintlsb0:4']))
# >>> [24, 16]