ajknzhol
ajknzhol

Reputation: 6460

AttributeError: 'str' object has no attribute 'decode' Python3

This line works perfectly in python 2.7.6, but fails in Python 3.3.5. How i can decode to hex value in Python 3.

return x.replace(' ', '').replace('\n', '').decode('hex')

Traceback

AttributeError: 'str' object has no attribute 'decode'

Upvotes: 5

Views: 20150

Answers (1)

anon582847382
anon582847382

Reputation: 20391

To convert hexadecimal to a string, use binascii.unhexlify.

>>> from binascii import unhexlify
>>> unhexlify(x.replace(' ', '').replace('\n', ''))

However, you first need to make x into bytes to make this work, for Python 3. Do this by doing:

>>> x = x.encode('ascii', 'strict')

And then do the hex-to-string conversion.

Upvotes: 2

Related Questions