Simple construction, analysis and modification of binary data.
Version: 1.0.1python-bitstring is a Python module for analysing, constructing and modifying binary data.
License: MIT/X Consortium License
Operating System: Linux
The underlying binary data can be interpreted as, or constructed from, hexadecimal, octal or binary strings, signed or unsigned integers, and signed or unsigned Exponential Golomb coded integers. It can also be used as and created from plain Python strings.
The module is provided with a comprehensive set of unit tests and has been tested with Python 2.4, 2.5 and 2.6.
Different interpretations, slicing and concatenation:
a = BitString(uint=5647, length=16)
print a.hex, a.bin, a.uint # 0x160f 0b0001011000001111 5647
print a[3:9].bin, a[3:9].int # 0b101100 -20
print (a[0:4] + '0xf').hex # 0x1f
Reading data sequentially. Note that the data would more usually come from a read() on a binary file.
b = BitString(data='x00x00x01xb3x16x01x20x4f')
start_code = b.readbytes(4).hex # 0x000001b3
width = b.readbits(12).uint # 352
height = b.readbits(12).uint # 288
flags = b.readbits(3).bin # 0b010
Searching, inserting and deleting:
c = BitString(bin='00010010010010001111') # c.hex == '0x1248f'
print c.readbyte() # 0x48
c.insert('0b0000') # c.hex == '0x12480f'
c.deletebits(bits=8, deletepos=4) # c.hex == '0x180f'