In Python 2.6 and later, you can use the built-in bytearray class.
To create a bytearray object:
b = bytearray.fromhex('deadbeef')
To change a byte, you can reference it using array notation:
b[2] += 7
To cancel bytearray in place, use b.reverse() . To create an iterator that iterates over it in reverse order, you can use the function reversed : reversed(b) .
You might also be interested in the new bytes class in Python 3, which is similar to bytearray but immutable.
source share