Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将二进制文件从大文件转换为小文件_Python_Python 3.x_Python 2.7_Endianness - Fatal编程技术网

Python 将二进制文件从大文件转换为小文件

Python 将二进制文件从大文件转换为小文件,python,python-3.x,python-2.7,endianness,Python,Python 3.x,Python 2.7,Endianness,我有一个大的二进制字节数组,我想把它从32位的大字节改为小字节 例如,b0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88到b0x44,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\x0

我有一个大的二进制字节数组,我想把它从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

通过这种方式,您可以在不复制数组的情况下更改字节顺序。

struct.unpack('>',…)
然后
struct.pack('这回答了您的问题吗?不,对于int,我有大字节数组