![](/img/trans.png)
[英]Is there a faster way to convert big file from hexa to binary and binary to int?
[英]Convert binary file from big to little endiand
我有大的二进制字节数组,我想从 32 位的大端更改为小端
例如 b 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88
到 b 0x44,0x33,0x22,0x11,0x88,0x77,0x66,0x55
我怎么能在 python 中做到这一点?
有很多方法可以做到这一点,这里是其中之一:
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
这样您就可以在不复制数组的情况下更改字节顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.