以这个例子为例:
i = 0x12345678
print("{:08x}".format(i))
# shows 12345678
i = swap32(i)
print("{:08x}".format(i))
# should print 78563412
如何编写swap32-function()
函数?有没有一种在Python中用内置工具进行int
字节交换的方法?
以这个例子为例:
i = 0x12345678
print("{:08x}".format(i))
# shows 12345678
i = swap32(i)
print("{:08x}".format(i))
# should print 78563412
如何编写swap32-function()
函数?有没有一种在Python中用内置工具进行int
字节交换的方法?
一种方法是使用struct
模块:
def swap32(i):
return struct.unpack("<I", struct.pack(">I", i))[0]
首先,您需要使用某种字节序将整数打包为二进制格式,然后使用另一种字节序对其进行解包(甚至无论您使用哪种组合,因为您只想交换字节序)。
{:08x}
的输出所迷惑,它省略了 0x
。12345678 != 0x12345678。 - Patrick B.struct.unpack("<I", struct.pack(">I", i))[0]
- Artem Zankovich大端字节序指的是32位整数的布局方式,最高有效字节排在第一位,
例如: 0x12345678 的存储布局为
msb lsb
+------------------+
| 12 | 34 | 56 | 78|
+------------------+
当使用小端序时,内存布局如下:
lsb msb
+------------------+
| 78 | 56 | 34 | 12|
+------------------+
所以您可以通过一些位掩码和移位来在它们之间进行转换:
def swap32(x):
return (((x << 24) & 0xFF000000) |
((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) |
((x >> 24) & 0x000000FF))
从Python 3.2开始,您可以将函数swap32()定义为以下内容:
在Python 3.2及以上版本中,可以按照以下方式定义swap32()函数:
def swap32(x):
return int.from_bytes(x.to_bytes(4, byteorder='little'), byteorder='big', signed=False)
它使用字节数组来表示这个值,并通过在转换回整数时改变字节序来反转字节的顺序。
from socket import htonl
swapped = htonl (i)
print (hex(swapped))
就是这样。这个库还可以通过ntohl在另一个方向上工作。
Array 模块提供了一个 byteswap() 方法,用于固定大小的项目。 该数组模块似乎在 Python 2.7 版本中就已经存在了。
array.byteswap() “Byteswap” 数组的所有项目。这仅支持大小为 1、2、4 或 8 字节的值;
除了 fromfile() 和 tofile() 方法外,该模块非常容易使用:
import array
# Open a data file.
input_file = open( 'my_data_file.bin' , 'rb' )
# Create an empty array of unsigned 4-byte integers.
all_data = array.array( 'L' )
# Read all the data from the file.
all_data.fromfile( input_file , 16000 ) # assumes the size of the file
# Swap the bytes in all the data items.
all_data.byteswap( )
# Write all the data to a new file.
output_file = open( filename[:-4] + '.new' , 'wb' ) # assumes a three letter extension
all_data.tofile( output_file )
# Close the files.
input_file.close( )
output_file_close( )
上述代码对我有用,因为我有固定大小的数据文件。有更多Pythonic的方法来处理可变长度的文件。
array.byteswap()
方法。 - aruisdante{ 12 34 56 78 }
,它将变成{ 78 56 34 12 }
。 - AaronF