HEX decoding in Python 3.2

2024/10/12 10:22:37

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?

Answer

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://en.xdnf.cn/q/69663.html

Related Q&A

How do I access session data in Jinja2 templates (Bottle framework on app engine)?

Im running the micro framework Bottle on Google App Engine. Im using Jinja2 for my templates. And Im using Beaker to handle the sessions. Im still a pretty big Python newbie and am pretty stoked I g…

What is a dimensional range of [-1,0] in Pytorch?

So Im struggling to understand some terminology about collections in Pytorch. I keep running into the same kinds of errors about the range of my tensors being incorrect, and when I try to Google for a …

Pyinstaller executable keeps opening

Background I am working on face recognition following this link and I would like to build a standalone application using Python. My main.py script looks like the following. # main.py# Import packages a…

Occasional deadlock in multiprocessing.Pool

I have N independent tasks that are executed in a multiprocessing.Pool of size os.cpu_count() (8 in my case), with maxtasksperchild=1 (i.e. a fresh worker process is created for each new task). The mai…

How to kill a subprocess called using subprocess.call in python? [duplicate]

This question already has answers here:How to terminate a python subprocess launched with shell=True(11 answers)Closed 7 years ago.I am calling a command using subprocess.call(cmd, shell=True)The comma…

Printing one color using imshow [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Send headers along in python [duplicate]

This question already has answers here:Changing user agent on urllib2.urlopen(9 answers)Closed 9 years ago.I have the following python script and I would like to send "fake" header informatio…

How to download image to memory using python requests?

Like here How to download image using requests , but to memory, using http://docs.python-requests.org.

Accessing NumPy record array columns in Cython

Im a relatively experienced Python programmer, but havent written any C in a very long time and am attempting to understand Cython. Im trying to write a Cython function that will operate on a column o…

scipy append all rows of one sparse matrix to another

I have a numpy matrix and want to append another matrix to that.The two matrices have the shapes:m1.shape = (2777, 5902) m2.shape = (695, 5902)I want to append m2 to m1 so that the new matrix is of sh…