hello, I want to change n bytes of a binary file
Ganesan Rajagopal
rganesan at myrealbox.com
Wed Nov 2 07:29:37 EST 2005
>>>>> "Fredrik" == Fredrik Lundh <fredrik at pythonware.com> writes:
> if you want to XOR the bytes, you can do something like:
> import array
> b = array.array("B", a)
> for i in range(len(b)):
> b[i] = b[i] ^ 255
> b = b.tostring()
> or
> b = "".join([chr(ord(c) ^ 255) for c in a])
I wish python would soon get a byte array type. For now I find numarray to
be very convenient to do stuff like this.
import numarray
b = (numarray.array(a, type = 'u1') ^ 255).tostring()
It's also significantly faster for large byte array sizes.
Ganesan
--
Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
More information about the Python-list
mailing list