简体   繁体   English

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

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

I'm trying to unapck a list from hex to integers in python.我正在尝试在 python 中将一个从十六进制到整数的列表解包。

So for example:例如:

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

Which would print (144,) and nothing else.这将打印 (144,) 而没有别的。 Is there any way I can loop through this string to get all values?有什么办法可以遍历这个字符串来获取所有值吗? (bear in mind the length of hex values is much longer than the example given.) (请记住,十六进制值的长度比给出的示例长得多。)

You can get all the values at once:您可以一次获取所有值:

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)

As Jon Clements helpfully pointed out in a comment, you don't really need to use the struct module:正如 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