0

I'm tring to write equivalent for encode("hex") in python3.

Here what i got:

s = "'"
print(str(s).encode('hex'))
>>27

Tring binascii in Python 2.7:

import binascii

s = "'"
print(binascii.hexlify(str(s)))
>>27

So in Python 2.7 both methods give same result. Now i tring to run this code in Python 3.5:

import binascii

s = "'"
print(binascii.hexlify(str(s)))

>>Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

After i tried:

import binascii

s = "'"
print(binascii.hexlify(str(s).encode('utf8')))

>>b'27'

But not sure what i have to do next. How to get my 27?

Kliver Max
  • 5,107
  • 22
  • 95
  • 148

1 Answers1

1

You only need to decode it, like this:

binascii.hexlify(str(s).encode('utf8')).decode('utf8')
marcos
  • 4,473
  • 1
  • 10
  • 24