簡體   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