work with binary data in python

Python provides two built-in types to work with binary data: bytes and bytearray. Both types represent sequences of bytes, with bytes being immutable and bytearray being mutable.

To create a bytes object, you can simply enclose a sequence of bytes in single quotes b'' or double quotes b"":

main.py
>>> b = b'\x00\x01\x02' # create bytes object
>>> b
b'\x00\x01\x02'
>>> type(b)
<class 'bytes'>
96 chars
6 lines

Alternatively, you can use the bytes() constructor to create a bytes object:

main.py
>>> b = bytes([0, 1, 2])
>>> b
b'\x00\x01\x02'
47 chars
4 lines

To modify binary data, you can use bytearrays:

main.py
>>> ba = bytearray(b'\x00\x01\x02') # create bytearray from bytes object
>>> ba[0] = 255  # modify first byte
>>> ba
bytearray(b'\xff\x01\x02')
144 chars
5 lines

When working with binary data, you may need to pack and unpack binary data representing C structs. Python provides the struct module for this purpose:

main.py
>>> import struct
>>> data = struct.pack('hhl', 1, 2, 3)  # pack data into bytes object
>>> data
b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
>>> values = struct.unpack('hhl', data) # unpack data from bytes object
>>> values
(1, 2, 3)
258 chars
8 lines

In this example, the pack() method takes a format string ('hhl') and a set of values to pack. The format string specifies the type and ordering of the values. The unpack() method does the opposite, taking a format string and a bytes object and returning a tuple of the unpacked values.

gistlibby LogSnag