Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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'
like image 1000
ajknzhol Avatar asked Nov 01 '22 01:11

ajknzhol


1 Answers

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.

like image 169
anon582847382 Avatar answered Nov 11 '22 15:11

anon582847382