在Python 3中解码十六进制字符串

时间:2022-01-05 11:20:27

In Python 2, converting the hexadecimal form of a string into the corresponding unicode was straightforward:

在Python 2中,将字符串的十六进制形式转换为相应的unicode非常简单:

comments.decode("hex")

where the variable 'comments' is a part of a line in a file (the rest of the line does not need to be converted, as it is represented only in ASCII.

其中变量'comments'是文件中一行的一部分(该行的其余部分不需要转换,因为它仅以ASCII表示。

Now in Python 3, however, this doesn't work (I assume because of the bytes/string vs. string/unicode switch. I feel like there should be a one-liner in Python 3 to do the same thing, rather than reading the entire line as a series of bytes (which I don't want to do) and then converting each part of the line separately. If it's possible, I'd like to read the entire line as a unicode string (because the rest of the line is in unicode) and only convert this one part from a hexadecimal representation.

然而,现在在Python 3中,这不起作用(我假设因为字节/字符串与字符串/ unicode开关。我觉得Python 3中应该有一个单行程来做同样的事情,而不是阅读整行作为一系列字节(我不想做),然后分别转换行的每一部分。如果可能的话,我想把整行读作unicode字符串(因为剩下的该行是unicode)并且只从十六进制表示转换这一部分。

2 个解决方案

#1


51  

Something like:

>>> bytes.fromhex('4a4b4c').decode('utf-8')
'JKL'

Just put the actual encoding you are using.

只需输入您正在使用的实际编码。

#2


1  

import codecs

decode_hex = codecs.getdecoder("hex_codec")

# for an array
msgs = [decode_hex(msg)[0] for msg in msgs]

# for a string
string = decode_hex(string)[0]

#1


51  

Something like:

>>> bytes.fromhex('4a4b4c').decode('utf-8')
'JKL'

Just put the actual encoding you are using.

只需输入您正在使用的实际编码。

#2


1  

import codecs

decode_hex = codecs.getdecoder("hex_codec")

# for an array
msgs = [decode_hex(msg)[0] for msg in msgs]

# for a string
string = decode_hex(string)[0]