[英]Python hexidecimal address packing from string
This works correctly: 这可以正常工作:
packed = struct.pack('<L',0x7c023a4f)
This does not: 这不是:
address = '0x7c023a4f'
packed = struct.pack('<L',address)
How do i make this work? 我该如何工作? I tried a lot of methods from the binascii library but i cannot seem to figure it out. 我尝试了binascii库中的许多方法,但似乎无法弄清楚。
You can use literal_eval
to evaluate the string as hex number before packing it: 您可以在打包之前使用literal_eval
将字符串评估为十六进制数字:
from ast import literal_eval
address = '0x7c023a4f'
packed = struct.pack('<L', literal_eval(address))
packed
# 'O:\x02|'
Convert it to an integer: 将其转换为整数:
address = '0x7c023a4f'
packed = struct.pack('<L', int(address, 16))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.