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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20806560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:16:19  来源:igfitidea点击:

How to display a byte array as hex values

python

提问by Siavash

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

我希望所有值都显示为十六进制

采纳答案by Lukas Graf

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. It's basically a very compact way of writing a simple forloop, and creating a list from the result.

该表达式[item for item in sequence]是所谓的列表推导式。它基本上是编写简单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.

它的对应物,chr()对于 8 位字符串或unichr()unicode 对象,则相反。

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']

回答by Tim Peters

You have to reformat it yourself if you want \xescapes everywhere; e.g.,

如果你想到处\x转义,你必须自己重新格式化;例如,

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

回答by Mark Tolonen

Try binascii.hexlify:

尝试binascii.hexlify

>>> import binascii
>>> import struct
>>> binascii.hexlify(struct.pack('2I',12,30))
b'0c0000001e000000'
>>> binascii.hexlify(struct.pack('2I',12,31))
b'0c0000001f000000'
>>> binascii.hexlify(struct.pack('2I',12,32))
b'0c00000020000000'
>>> binascii.hexlify(struct.pack('2I',12,33))
b'0c00000021000000'

Or if you want spaces to make it more readable:

或者,如果您想要空格使其更具可读性:

>>> ' '.join(format(n,'02X') for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'

Python 3.6+, using f-strings:

Python 3.6+,使用 f 字符串:

>>> ' '.join(f'{n:02X}' for n in struct.pack('2I',12,33))
'0C 00 00 00 21 00 00 00'

回答by Artem Zankovich

There is an option to convert byte array to hex string with encode. It works for any Python from Python 2.4:

有一个选项可以将字节数组转换为十六进制字符串encode。它适用于 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'

回答by kiamlaluno

In Python 3.7 bytes objects don't have an encode()method. The following code doesn't work anymore.

在 Python 3.7 字节对象中没有encode()方法。以下代码不再起作用。

import struct

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

Instead of encode(), Python 3.7 code should use the hex()method, introduced in Python 3.5.

encode()Python 3.7 代码应该使用hex()Python 3.5 中引入的方法,而不是。

import struct

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