问题:在Python 3中将字节转换为十六进制字符串的正确方法是什么?

在Python 3中将字节转换为十六进制字符串的正确方法是什么?

我看到了bytes.hex方法,bytes.decode编解码器的声明,并尝试了其他最小惊讶的可能功能,但没有用。我只希望字节为十六进制!

What’s the correct way to convert bytes to a hex string in Python 3?

I see claims of a bytes.hex method, bytes.decode codecs, and have tried other possible functions of least astonishment without avail. I just want my bytes as hex!


回答 0

从Python 3.5开始,这终于不再笨拙了:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

并反向:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

也适用于可变bytearray类型。

参考:https : //docs.python.org/3/library/stdtypes.html#bytes.hex

Since Python 3.5 this is finally no longer awkward:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

and reverse:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

works also with the mutable bytearray type.

Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex


回答 1

使用binascii模块:

>>> import binascii
>>> binascii.hexlify('foo'.encode('utf8'))
b'666f6f'
>>> binascii.unhexlify(_).decode('utf8')
'foo'

查看此答案: Python 3.1.1字符串转换为十六进制

Use the binascii module:

>>> import binascii
>>> binascii.hexlify('foo'.encode('utf8'))
b'666f6f'
>>> binascii.unhexlify(_).decode('utf8')
'foo'

See this answer: Python 3.1.1 string to hex


回答 2

Python具有逐个字节的标准编解码器,可以执行方便的转换,例如带引号的可打印(适合7位ascii),base64(适合字母数字),十六进制转义,gzip和bz2压缩。在Python 2中,您可以执行以下操作:

b'foo'.encode('hex')

在Python 3中,str.encode/ bytes.decode严格用于字节<-> str转换。相反,您可以执行此操作,该操作适用于Python 2和Python 3(反之为s / encode / decode / g):

import codecs
codecs.getencoder('hex')(b'foo')[0]

从Python 3.4开始,有一个不太尴尬的选项:

codecs.encode(b'foo', 'hex')

这些杂项编解码器也可以在它们自己的模块(base64,zlib,bz2,uu,quopri,binascii)中访问;API的一致性较差,但对于压缩编解码器,它提供了更多控制权。

Python has bytes-to-bytes standard codecs that perform convenient transformations like quoted-printable (fits into 7bits ascii), base64 (fits into alphanumerics), hex escaping, gzip and bz2 compression. In Python 2, you could do:

b'foo'.encode('hex')

In Python 3, str.encode / bytes.decode are strictly for bytes<->str conversions. Instead, you can do this, which works across Python 2 and Python 3 (s/encode/decode/g for the inverse):

import codecs
codecs.getencoder('hex')(b'foo')[0]

Starting with Python 3.4, there is a less awkward option:

codecs.encode(b'foo', 'hex')

These misc codecs are also accessible inside their own modules (base64, zlib, bz2, uu, quopri, binascii); the API is less consistent, but for compression codecs it offers more control.


回答 3

import codecs
codecs.getencoder('hex_codec')(b'foo')[0]

在Python 3.3中工作(因此是“ hex_codec”而不是“ hex”)。

import codecs
codecs.getencoder('hex_codec')(b'foo')[0]

works in Python 3.3 (so “hex_codec” instead of “hex”).


回答 4

该方法binascii.hexlify()将转换bytesbytes代表ascii十六进制字符串的字符串。这意味着输入中的每个字节将转换为两个ascii字符。如果您想要一个真实的结果str,那么您可以.decode("ascii")的结果得到结果。

我提供了一个说明它的片段。

import binascii

with open("addressbook.bin", "rb") as f: # or any binary file like '/bin/ls'
    in_bytes = f.read()
    print(in_bytes) # b'\n\x16\n\x04'
    hex_bytes = binascii.hexlify(in_bytes) 
    print(hex_bytes) # b'0a160a04' which is twice as long as in_bytes
    hex_str = hex_bytes.decode("ascii")
    print(hex_str) # 0a160a04

从十六进制字符串"0a160a04"到可以回来了bytesbinascii.unhexlify("0a160a04")该还给b'\n\x16\n\x04'

The method binascii.hexlify() will convert bytes to a bytes representing the ascii hex string. That means that each byte in the input will get converted to two ascii characters. If you want a true str out then you can .decode("ascii") the result.

I included an snippet that illustrates it.

import binascii

with open("addressbook.bin", "rb") as f: # or any binary file like '/bin/ls'
    in_bytes = f.read()
    print(in_bytes) # b'\n\x16\n\x04'
    hex_bytes = binascii.hexlify(in_bytes) 
    print(hex_bytes) # b'0a160a04' which is twice as long as in_bytes
    hex_str = hex_bytes.decode("ascii")
    print(hex_str) # 0a160a04

from the hex string "0a160a04" to can come back to the bytes with binascii.unhexlify("0a160a04") which gives back b'\n\x16\n\x04'


回答 5

好的,如果您只关心Python 3,以下答案会稍微超出范围,但是即使您未指定Python版本,此问题也是Google的第一个热门产品,因此这是一种适用于Python 2 Python 3的方法。

我也在解释这个问题是关于将字节转换为 str类型:即在Python 2上为bytes-y,在Python 3上为Unicode-y。

鉴于此,我知道的最佳方法是:

import six

bytes_to_hex_str = lambda b: ' '.join('%02x' % i for i in six.iterbytes(b))

假设您尚未激活unicode_literalsPython 2 的未来,以下断言对于Python 2或Python 3都是正确的:

assert bytes_to_hex_str(b'jkl') == '6a 6b 6c'

(或者您可以''.join()用来省略字节之间的空格,等等。)

OK, the following answer is slightly beyond-scope if you only care about Python 3, but this question is the first Google hit even if you don’t specify the Python version, so here’s a way that works on both Python 2 and Python 3.

I’m also interpreting the question to be about converting bytes to the str type: that is, bytes-y on Python 2, and Unicode-y on Python 3.

Given that, the best approach I know is:

import six

bytes_to_hex_str = lambda b: ' '.join('%02x' % i for i in six.iterbytes(b))

The following assertion will be true for either Python 2 or Python 3, assuming you haven’t activated the unicode_literals future in Python 2:

assert bytes_to_hex_str(b'jkl') == '6a 6b 6c'

(Or you can use ''.join() to omit the space between the bytes, etc.)


回答 6

可以使用格式说明符%x02来格式化并输出一个十六进制值。例如:

>>> foo = b"tC\xfc}\x05i\x8d\x86\x05\xa5\xb4\xd3]Vd\x9cZ\x92~'6"
>>> res = ""
>>> for b in foo:
...     res += "%02x" % b
... 
>>> print(res)
7443fc7d05698d8605a5b4d35d56649c5a927e2736

it can been used the format specifier %x02 that format and output a hex value. For example:

>>> foo = b"tC\xfc}\x05i\x8d\x86\x05\xa5\xb4\xd3]Vd\x9cZ\x92~'6"
>>> res = ""
>>> for b in foo:
...     res += "%02x" % b
... 
>>> print(res)
7443fc7d05698d8605a5b4d35d56649c5a927e2736

回答 7

python 3.8中的新增功能,您可以将定界符参数传递给hex函数,如本例所示

>>> value = b'\xf0\xf1\xf2'
>>> value.hex('-')
'f0-f1-f2'
>>> value.hex('_', 2)
'f0_f1f2'
>>> b'UUDDLRLRAB'.hex(' ', -4)
'55554444 4c524c52 4142'

https://docs.python.org/3/library/stdtypes.html#bytes.hex

New in python 3.8, you can pass a delimiter argument to the hex function, as in this example

>>> value = b'\xf0\xf1\xf2'
>>> value.hex('-')
'f0-f1-f2'
>>> value.hex('_', 2)
'f0_f1f2'
>>> b'UUDDLRLRAB'.hex(' ', -4)
'55554444 4c524c52 4142'

https://docs.python.org/3/library/stdtypes.html#bytes.hex


回答 8

如果要将b’\ x61’转换为97或’0x61’,可以尝试以下操作:

[python3.5]
>>>from struct import *
>>>temp=unpack('B',b'\x61')[0] ## convert bytes to unsigned int
97
>>>hex(temp) ##convert int to string which is hexadecimal expression
'0x61'

参考:https : //docs.python.org/3.5/library/struct.html

If you want to convert b’\x61′ to 97 or ‘0x61’, you can try this:

[python3.5]
>>>from struct import *
>>>temp=unpack('B',b'\x61')[0] ## convert bytes to unsigned int
97
>>>hex(temp) ##convert int to string which is hexadecimal expression
'0x61'

Reference:https://docs.python.org/3.5/library/struct.html


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。