繁体   English   中英

将十六进制值的列表/字符串解包为整数

[英]Unpacking a list/string of hex values into integers

我正在尝试在 python 中将一个从十六进制到整数的列表解包。

例如:

hexValues = '\x90\x82|uj\x82ix'
decodedHex = struct.unpack_from('B', hexValues,0)
print decodedHex

这将打印 (144,) 而没有别的。 有什么办法可以遍历这个字符串来获取所有值吗? (请记住,十六进制值的长度比给出的示例长得多。)

您可以一次获取所有值:

import struct

hexValues = '\x90\x82|uj\x82ix'
format = '%dB' % len(hexValues)
decodedHex = struct.unpack_from(format, hexValues)
print(decodedHex)  # -> (144, 130, 124, 117, 106, 130, 105, 120)

正如 Jon Clements 在评论中帮助指出的那样,您实际上并不需要使用struct模块:

decodedHex = tuple(bytearray(hexValues))
print(decodedHex)  # -> (144, 130, 124, 117, 106, 130, 105, 120)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM