Python 3.1.1 string to hex
Solution 1
The hex
codec has been chucked in 3.x. Use binascii
instead:
>>> binascii.hexlify(b'hello')
b'68656c6c6f'
Solution 2
In Python 3.5+, encode the string to bytes and use the hex()
method, returning a string.
s = "hello".encode("utf-8").hex()
s
# '68656c6c6f'
Optionally convert the string back to bytes:
b = bytes(s, "utf-8")
b
# b'68656c6c6f'
Solution 3
You've already got some good answers, but I thought you might be interested in a bit of the background too.
Firstly you're missing the quotes. It should be:
"hello".encode("hex")
Secondly this codec hasn't been ported to Python 3.1. See here. It seems that they haven't yet decided whether or not these codecs should be included in Python 3 or implemented in a different way.
If you look at the diff file attached to that bug you can see the proposed method of implementing it:
import binascii
output = binascii.b2a_hex(input)
Solution 4
binascii methodes are easier by the way
>>> import binascii
>>> x=b'test'
>>> x=binascii.hexlify(x)
>>> x
b'74657374'
>>> y=str(x,'ascii')
>>> y
'74657374'
>>> x=binascii.unhexlify(x)
>>> x
b'test'
>>> y=str(x,'ascii')
>>> y
'test'
Hope it helps. :)
Solution 5
The easiest way to do it in Python 3.5 and higher is:
>>> 'halo'.encode().hex()
'68616c6f'
If you manually enter a string into a Python Interpreter using the utf-8
characters, you can do it even faster by typing b
before the string:
>>> b'halo'.hex()
'68616c6f'
Equivalent in Python 2.x:
>>> 'halo'.encode('hex')
'68616c6f'
Stuart
Updated on July 05, 2022Comments
-
Stuart almost 2 years
I am trying to use
str.encode()
but I get>>> "hello".encode(hex) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be string, not builtin_function_or_method
I have tried a bunch of variations and they seem to all work in Python 2.5.2, so what do I need to do to get them to work in Python 3.1?
-
Mark Tolonen about 14 yearsIt was shorter than "hexlificationize" :^)
-
TrinitronX about 10 yearsThis is exactly what I needed! A cross-python version way of hex encoding & decoding. Thanks ^_^
>>> import base64 >>> key = base64.b16encode(b'0123456789abcdef') >>> base64.b16decode(key) '0123456789abcdef'
-
Simon Steinberger over 8 yearsbinascii is also faster than the other methods. Just tested with timeit.
-
tutuDajuju over 8 yearsBTW, binary codes made a comeback in version 3.2, see docs
-
Jari Turkia about 6 yearsHow do you do string to hex conversion, if the string is a regular Python 3 string, not binary or a constant?
-
Jari Turkia about 6 yearsThis the way to do it for a Python 3 string variable. Lot of the answers here are regarding constants. A practical code is rarely that.
-
nabil london almost 6 yearsWhat if my string is stored in a variable?
-
nabil london almost 6 yearsThat gives an error, because the argument has to be a bytes-like object.
-
ner0 over 4 yearsEmphasis on >=Python 3.5 if you want to use
hex()
-
simhumileco over 4 yearsThank you @ner0 for your important comment!
-
ner0 over 4 yearsNo worries. If v3.4 or below is a requirement, this will do:
from binascii import hexlify str(hexlify(bytes('halo', encoding = 'utf-8')), 'ascii')
-
PixelRayn about 3 yearsTo convert a String to binary sequence you can use ".encode()". Example: "This is your String".encode()
-
Niels Abildgaard about 2 yearsWhy is this better than e.g.
b'hello'.hex()
? -
Niels Abildgaard about 2 yearsIs this because
.hex()
wasn't added until Python 3.5? A link to the changelog would have been really good in this answer as context :-) -
Eloff almost 2 yearsWhat was the point of breaking backward compatibility like that? I don't understand the mindset.