简体   繁体   English

"如何将字节数组显示为十六进制值"

[英]How to display a byte array as hex values

>>> struct.pack('2I',12, 30)
b'\x0c\x00\x00\x00\x1e\x00\x00\x00'    
>>> struct.pack('2I',12, 31)
b'\x0c\x00\x00\x00\x1f\x00\x00\x00'
>>> struct.pack('2I',12, 32)
b'\x0c\x00\x00\x00 \x00\x00\x00'
                  ^in question
>>> struct.pack('2I',12, 33)
b'\x0c\x00\x00\x00!\x00\x00\x00'
                  ^in question

I'd like all values to display as hex我希望所有值都显示为十六进制

See bytes.hex()<\/code> :请参阅bytes.hex()<\/code> :

>>> import struct
>>> struct.pack('2I',12,30).hex()   # available since Python 3.5
'0c0000001e000000'
>>> struct.pack('2I',12,30).hex(' ')  # separator available since Python 3.8
'0c 00 00 00 1e 00 00 00'
>>> struct.pack('2I',12,30).hex(' ',4) # bytes_per_sep also since Python 3.8
'0c000000 1e000000'

How about his?他的呢?

>>> data = struct.pack('2I',12, 30)
>>> [hex(ord(c)) for c in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']

The expression [item for item in sequence] is a so called list comprehension .表达式[item for item in sequence]是所谓的列表推导 It's basically a very compact way of writing a simple for loop, and creating a list from the result.它基本上是一种非常紧凑的方式来编写一个简单for循环,并从结果中创建一个列表。

The ord() builtin function takes a string, and turns it into an integer that's its corresponding unicode code point (for characters in the ASCII character set that's the same as their value in the ASCII table). ord()内置函数接受一个字符串,并将其转换为一个整数,该整数是其对应的 unicode 代码点(对于 ASCII 字符集中的字符,与它们在 ASCII 表中的值相同)。

Its counterpart, chr() for 8bit strings or unichr() for unicode objects do the opposite.它的对应物,用于 8 位字符串的chr()或用于 unicode 对象的unichr()则相反。

The hex() builtin then simply converts the integers into their hex representation.然后内置的hex()简单地将整数转换为它们的十六进制表示。


As pointed out by @TimPeters, in Python 3 you would need to lose the ord() , because iterating over a bytes object will (already) yield integers:正如@TimPeters 所指出的,在 Python 3 中,您需要丢失ord() ,因为迭代字节对象将(已经)产生整数:

Python 3.4.0a3 (default, Nov  8 2013, 18:33:56)
>>> import struct
>>> data = struct.pack('2I',12, 30)
>>> type(data)
<class 'bytes'>
>>> type(data[1])
<class 'int'>
>>>
>>> [hex(i) for i in data]
['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']

You have to reformat it yourself if you want \\x<\/code> escapes everywhere;如果你想\\x<\/code>到处转义,你必须自己重新格式化它; eg,例如,

>>> import struct
>>> r = struct.pack('2I',12, 33)
>>> r
b'\x0c\x00\x00\x00!\x00\x00\x00'
>>> list(r)
[12, 0, 0, 0, 33, 0, 0, 0]
>>> print("".join("\\x%02x" % i for i in r))
\x0c\x00\x00\x00\x21\x00\x00\x00

In Python 3.7 bytes objects don't have an encode() method.在 Python 3.7 字节对象中没有encode()方法。 The following code doesn't work anymore.以下代码不再起作用。

import struct

hex_str = struct.pack('2I',12, 30).encode('hex')

Instead of encode() , Python 3.7 code should use thehex() method, introduced in Python 3.5. Python 3.7 代码应该使用 Python 3.5 中引入的hex()方法,而不是encode()

import struct

# hex_str will contain the '0c0000001e000000' string.
hex_str = struct.pack('2I',12, 30).hex()

There is an option to convert byte array to hex string with encode<\/code> .有一个选项可以使用encode<\/code>将字节数组转换为十六进制字符串。 It works for any Python from Python 2.4:它适用于 Python 2.4 中的任何 Python:

Python 2.7.12 (default, Oct 10 2016, 12:50:22)
>>> import struct
>>> struct.pack('2I',12, 30).encode('hex')
'0c0000001e000000'
>>> struct.pack('2I',12, 31).encode('hex')
'0c0000001f000000'
>>> struct.pack('2I',12, 32).encode('hex')
'0c00000020000000'
>>> struct.pack('2I',12, 33).encode('hex')
'0c00000021000000'

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

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