Personally, I would use the bitstring module, but I can be biased as I wrote it. As an example, you can find simple code for reading / writing binary format in the manual .
This is one way to create via binary format:
fmt = 'sequence_header_code, uint:12=horizontal_size_value, uint:12=vertical_size_value, uint:4=aspect_ratio_information, ... ' d = {'sequence_header_code': '0x000001b3', 'horizontal_size_value': 352, 'vertical_size_value': 288, 'aspect_ratio_information': 1, ... } s = bitstring.pack(fmt, **d)
and one method for analyzing it:
>>> s.unpack('bytes:4, 2*uint:12, uint:4') ['\x00\x00\x01\xb3', 352, 288, 1]
Scott griffiths
source share