Python 3.1.1 string to hex

178,795

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'
Share:
178,795
Stuart
Author by

Stuart

Updated on July 05, 2022

Comments

  • Stuart
    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
    Mark Tolonen about 14 years
    It was shorter than "hexlificationize" :^)
  • TrinitronX
    TrinitronX about 10 years
    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'
  • Simon Steinberger
    Simon Steinberger over 8 years
    binascii is also faster than the other methods. Just tested with timeit.
  • tutuDajuju
    tutuDajuju over 8 years
    BTW, binary codes made a comeback in version 3.2, see docs
  • Jari Turkia
    Jari Turkia about 6 years
    How do you do string to hex conversion, if the string is a regular Python 3 string, not binary or a constant?
  • Jari Turkia
    Jari Turkia about 6 years
    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.
  • nabil london
    nabil london almost 6 years
    What if my string is stored in a variable?
  • nabil london
    nabil london almost 6 years
    That gives an error, because the argument has to be a bytes-like object.
  • ner0
    ner0 over 4 years
    Emphasis on >=Python 3.5 if you want to use hex()
  • simhumileco
    simhumileco over 4 years
    Thank you @ner0 for your important comment!
  • ner0
    ner0 over 4 years
    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')
  • PixelRayn
    PixelRayn about 3 years
    To convert a String to binary sequence you can use ".encode()". Example: "This is your String".encode()
  • Niels Abildgaard
    Niels Abildgaard about 2 years
    Why is this better than e.g. b'hello'.hex()?
  • Niels Abildgaard
    Niels Abildgaard about 2 years
    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 :-)
  • Eloff
    Eloff almost 2 years
    What was the point of breaking backward compatibility like that? I don't understand the mindset.