HEX decoding in Python 3.2

我的梦境 提交于 2019-12-10 12:59:18

问题


In Python 2.x I'm able to do this:

>>> '4f6c6567'.decode('hex_codec')
'Oleg'

But in Python 3.2 I encounter this error:

>>> b'4f6c6567'.decode('hex_codec')
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    b'4f6c6567'.decode('hex_codec')
TypeError: decoder did not return a str object (type=bytes)

According to the docs hex_codec should provide "bytes-to-bytes mappings". So the object of byte-string is correctly used here.

How can I get rid of this error to be able to avoid unwieldy workarounds to convert from hex-encoded text?


回答1:


In Python 3, the bytes.decode() method is used to decode raw bytes to Unicode, so you have to get the decoder from the codecs module using codecs.getdecoder() or codecs.decode() for bytes-to-bytes encodings:

>>> codecs.decode(b"4f6c6567", "hex_codec")
b'Oleg'
>>> codecs.getdecoder("hex_codec")(b"4f6c6567")
(b'Oleg', 8)

The latter function seems to be missing from the documentation, but has a useful docstring.

You might also want to have a look at binascii.unhexlify().



来源:https://stackoverflow.com/questions/11384583/hex-decoding-in-python-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!