bitstring icon indicating copy to clipboard operation
bitstring copied to clipboard

Support specifying MSB0 or LSB0 when reading ints from a BitStream

Open medley56 opened this issue 3 years ago • 0 comments

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]

medley56 avatar Feb 17 '22 19:02 medley56