How to pack arbitrary bit sequence in Python?

后端 未结 3 2129
南笙
南笙 2021-01-06 06:56

I want to encode/compress some binary image data as a sequence if bits. (This sequence will, in general, have a length that does not fit neatly in a whole number of standar

3条回答
  •  灰色年华
    2021-01-06 07:18

    There's nothing very convenient built in but there are third-party modules such as bitstring and bitarray which are designed for this.

    from bitstring import BitArray
    s = BitArray('0b11011')
    s += '0b100'
    s += 'uint:5=9'
    s += [0, 1, 1, 0, 1]
    ...
    s.tobytes()
    

    To join together a sequence of 3-bit numbers (i.e. range 0->7) you could use

    >>> symbols = [0, 4, 5, 3, 1, 1, 7, 6, 5, 2, 6, 2]
    >>> BitArray().join(BitArray(uint=x, length=3) for x in symbols)
    BitArray('0x12b27eab2')
    >>> _.tobytes()
    '\x12\xb2~\xab '
    

    Some related questions:

    • What is the best way to do Bit Field manipulation in Python?
    • Python Bitstream implementations

提交回复
热议问题