88

In Python 2 these all worked:

>>> "hello".encode("hex")
'68656c6c6f'
>>> b"hello".encode("hex")
'68656c6c6f'
>>> u"hello".encode("hex")
'68656c6c6f'

But in Python 3:

>>> "hello".encode("hex")
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs

How to convert string to hex in Python 3?

10 Answers 10

108

The hex codec has been chucked in 3.x. Use binascii instead:

>>> binascii.hexlify(b'hello')
b'68656c6c6f'
Sign up to request clarification or add additional context in comments.

6 Comments

BTW, binary codes made a comeback in version 3.2, see docs
What if my string is stored in a variable?
That gives an error, because the argument has to be a bytes-like object.
To convert a String to binary sequence you can use ".encode()". Example: "This is your String".encode()
Why is this better than e.g. b'hello'.hex()?
|
57

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'

2 Comments

This 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.
Emphasis on >=Python 3.5 if you want to use hex()
28

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'

3 Comments

Thank you @ner0 for your important comment!
No worries. If v3.4 or below is a requirement, this will do: from binascii import hexlify str(hexlify(bytes('halo', encoding = 'utf-8')), 'ascii')
Is 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 :-)
26

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)

Comments

24

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. :)

2 Comments

binascii is also faster than the other methods. Just tested with timeit.
How do you do string to hex conversion, if the string is a regular Python 3 string, not binary or a constant?
10

In Python 3, all strings are unicode. Usually, if you encode an unicode object to a string, you use .encode('TEXT_ENCODING'), since hex is not a text encoding, you should use codecs.encode() to handle arbitrary codecs. For example:

>>>> "hello".encode('hex')
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs
>>>> import codecs
>>>> codecs.encode(b"hello", 'hex')
b'68656c6c6f'

Again, since "hello" is unicode, you need to indicate it as a byte string before encoding to hexadecimal. This may be more inline with what your original approach of using the encode method.

The differences between binascii.hexlify and codecs.encode are as follow:

  • binascii.hexlify

    Hexadecimal representation of binary data.

    The return value is a bytes object.

    Type: builtin_function_or_method

  • codecs.encode

    encode(obj, [encoding[,errors]]) -> object

    Encodes obj using the codec registered for encoding. encoding defaults to the default encoding. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a ValueError. Other possible values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name registered with codecs.register_error that can handle ValueErrors.

    Type: builtin_function_or_method

Comments

8

base64.b16encode and base64.b16decode convert bytes to and from hex and work across all Python versions. The codecs approach also works, but is less straightforward in Python 3.

1 Comment

This 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'
3

Use hexlify - http://epydoc.sourceforge.net/stdlib/binascii-module.html

1 Comment

It was shorter than "hexlificationize" :^)
3

str to hex:

>>> "hello".encode().hex()
'68656c6c6f'

bytes to hex:

>>> b"hello".hex()
'68656c6c6f'

bytes from hex:

>>> bytes.fromhex("68656c6c6f")
b'hello'

str from hex:

>>> bytes.fromhex("68656c6c6f").decode()
'hello'

Comments

2

Yet another method:

s = 'hello'

h = ''.join([hex(ord(i)) for i in s]);

# outputs: '0x680x650x6c0x6c0x6f'

This basically splits the string into chars, does the conversion through hex(ord(char)), and joins the chars back together. In case you want the result without the prefix 0x then do:

h = ''.join([str(hex(ord(i)))[2:4] for i in s]);

# outputs: '68656c6c6f'

Tested with Python 3.5.3.

Comments

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.