5

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'
4
  • Maybe duplicated: see this post Commented Apr 12, 2014 at 17:30
  • 3
    The Heartbleed Python script is written for Python 2, not Python 3 :-) You'll need to address more issues other than the payload definition. Commented Apr 12, 2014 at 17:40
  • @MartijnPieters I sense that we are going to be getting many similar quetsions very soon... Commented Apr 12, 2014 at 17:45
  • 1
    @rcomp Note that the script has been ported to Python 3 by the github community: gist.github.com/dyatlov/10192468 Commented Apr 12, 2014 at 17:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Looking at the original source the point is to produce bytes to send over a socket. The decode isn't needed in that case.
NP, I was just answering a different question about the same script and recognized the line.
note: x.encode('ascii', 'strict') is not necessary on Python before/after 3.2
@J.F.Sebastian I don't quite understand you, but would what I have done in my edit be preferrable?
unhexlify() doesn't work with str objects in Python 3.2. Assuming that x contains hexdigits then you could use x.encode('ascii', 'strict') to convert str object to bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.