Reputation: 111
I have big binary bytearray that I want to change from big to little endian of 32 bits
For example b 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88
to b 0x44,0x33,0x22,0x11,0x88,0x77,0x66,0x55
How can I do that in python?
Upvotes: 0
Views: 692
Reputation: 19
There are many ways to do this, here is one of them:
data = bytearray(b'\x01\x02\x03\x04\x05\x06\x07\x08')
ref = bytearray(b'\x04\x03\x02\x01\x08\x07\x06\x05')
for offset in range(0, len(data), 4):
chunk = data[offset:offset + 4]
if len(chunk) != 4:
raise ValueError('alignment error')
data[offset:offset + 4] = chunk[::-1]
assert data == ref
This way you can change the byte order without copying the array.
Upvotes: 1