如何使用结构模块转换64位地址



使用Python的struct模块,我能够很好地转换32位地址:

rp = struct.pack("<L", 0x565555c7)
# b'xc7UUV'

但当我尝试64位地址时:

Traceback (most recent call last):
File "<string>", line 3, in <module>
struct.error: 'L' format requires 0 <= number <= 4294967295

那么,如果有,我该如何使用结构库呢?还有哪些其他方法可用于打包64位地址?

int有一种方法可以帮你做到这一点:

>>> 0x565555c7.to_bytes(8, 'big')
b'x00x00x00x00VUUxc7'

给定所需的字节数和字节序,to_bytes将生成一个bytes值。比较

# 4 bytes instead of 8
>>> 0x565555c7.to_bytes(4, 'big')
b'VUUxc7'
# 4 bytes, but little-endian instead of big-endian
>>> 0x565555c7.to_bytes(4, 'little')
b'xc7UUV'

L用于4字节(32位(无符号整数;Q用于8字节(64位(无符号整数。

最新更新