python - 如何使用 struct 模块转换 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'\x00\x00\x00\x00VUU\xc7'
to_bytes
bytes
在给定所需字节数和字节序的情况下生成一个值。相比
# 4 bytes instead of 8
>>> 0x565555c7.to_bytes(4, 'big')
b'VUU\xc7'
# 4 bytes, but little-endian instead of big-endian
>>> 0x565555c7.to_bytes(4, 'little')
b'\xc7UUV'
推荐阅读
- python - 简单的 SFTP 脚本
- javascript - PHP动态列表检索返回未定义索引
- scala - Lisp 宏和 scala 宏有什么区别?
- ruby-on-rails - 如何在 ActiveRecord 中的客户表中获取最受欢迎的邮政编码?
- perl - 为什么在生成守护进程时会剥离 utf8 IO 层?
- jquery - 无法让 Bootstrap 日期选择器显示
- sonarqube - SonarCloud:运行 ant 和连接声纳云的声纳时出错
- react-native - 反应本机铃声管理器
- python - 如何在 Flask 的网页上读取用户输入
- python - 如何检查 json 文件对象的条件?