376

What's the correct way to convert bytes to a hex string in Python 3?

I see claims of a bytes.hex method, bytes.decode codecs, and have tried other possible functions of least astonishment without avail. I just want my bytes as hex!

agf
  • 171,228
  • 44
  • 289
  • 238
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 2
    "without avail"? What **specific** problems or errors are you getting? Please show code and errors. – S.Lott Jul 08 '11 at 12:36
  • Possible duplicate of http://stackoverflow.com/questions/2340319/python-3-1-1-string-to-hex – Mu Mind Jul 08 '11 at 12:39

9 Answers9

720

Since Python 3.5 this is finally no longer awkward:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

and reverse:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

works also with the mutable bytearray type.

Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Felix Weis
  • 7,434
  • 1
  • 12
  • 6
  • 18
    [`bytes.fromhex()`](https://docs.python.org/3/library/stdtypes.html#bytes.fromhex) is also available on Python 3.0+ (not just 3.5+). `bytes.hex()` is only on Python 3.5+. – phoenix Jun 13 '18 at 14:16
109

Use the binascii module:

>>> import binascii
>>> binascii.hexlify('foo'.encode('utf8'))
b'666f6f'
>>> binascii.unhexlify(_).decode('utf8')
'foo'

See this answer: Python 3.1.1 string to hex

Community
  • 1
  • 1
Mu Mind
  • 10,935
  • 4
  • 38
  • 69
  • 11
    This is good. Mind boggling is that you can convert hex to bytes using bytes.fromhex(hex_str), but you cannot convert bytes to hex using bytes.tohex() - what is the rational in this? – nagylzs Nov 16 '13 at 21:57
  • 1
    I guess the relationship between bytes and hex isn't a property of either (which doesn't answer why fromhex is there). Seems like it wasn't just an oversight but something that was argued over: http://bugs.python.org/issue3532#msg70950. Q: Would it hurt to have the tohex method of the bytes object to perform this task as well? A: IMO, yes, it would. It complicates the code, and draws the focus away from the proper approach to data conversion (namely, functions - not methods). – Mu Mind Nov 17 '13 at 01:42
  • 6
    Does this really answer the question? It doesn't return a hex `str` but a `bytes`. I know that the OP seems happy with the answer but won't be better to expand this answer to include `.decode("ascii")` also to convert it to a "string" – RubenLaguna Jun 08 '15 at 17:11
  • @ecerulm Updated to include encoding/decoding the `'foo'`. I left the `b'666f6f'`, though… a byte string is the most natural representation of encoded hex. – Mu Mind Jun 08 '15 at 17:46
  • 4
    I was thinking that many people land on this question/answer looking for a way to printout a `bytes`. If you `print(b'666f6f')` you get the `b` in the printout. If you `.decode("ascii")` then you don't. Just thinking on how those that actually had a `bytes` (true binary with items > 128 , not an ascii string) an wanted to printout it. – RubenLaguna Jun 09 '15 at 05:52
  • 9
    @nagylzs: there is `.hex()` method in Python 3.5+ – jfs Feb 24 '16 at 15:39
  • Great (although I think Python 3.5 was not available at the time the question was asked) – nagylzs Feb 24 '16 at 16:03
52

Python has bytes-to-bytes standard codecs that perform convenient transformations like quoted-printable (fits into 7bits ascii), base64 (fits into alphanumerics), hex escaping, gzip and bz2 compression. In Python 2, you could do:

b'foo'.encode('hex')

In Python 3, str.encode / bytes.decode are strictly for bytes<->str conversions. Instead, you can do this, which works across Python 2 and Python 3 (s/encode/decode/g for the inverse):

import codecs
codecs.getencoder('hex')(b'foo')[0]

Starting with Python 3.4, there is a less awkward option:

codecs.encode(b'foo', 'hex')

These misc codecs are also accessible inside their own modules (base64, zlib, bz2, uu, quopri, binascii); the API is less consistent, but for compression codecs it offers more control.

Gabriel
  • 1,262
  • 12
  • 12
32

New in python 3.8, you can pass a delimiter argument to the hex function, as in this example

>>> value = b'\xf0\xf1\xf2'
>>> value.hex('-')
'f0-f1-f2'
>>> value.hex('_', 2)
'f0_f1f2'
>>> b'UUDDLRLRAB'.hex(' ', -4)
'55554444 4c524c52 4142'

https://docs.python.org/3/library/stdtypes.html#bytes.hex

Peter Mitrano
  • 2,132
  • 28
  • 31
  • 1
    This can be useful: `[f'0x{val}' for val in value.hex(' ', 2).split()]` gives something like `['0x2600', '0x9c00', '0x1900', '0x0c75']` – Dennis Williamson Apr 28 '22 at 14:10
12

The method binascii.hexlify() will convert bytes to a bytes representing the ascii hex string. That means that each byte in the input will get converted to two ascii characters. If you want a true str out then you can .decode("ascii") the result.

I included an snippet that illustrates it.

import binascii

with open("addressbook.bin", "rb") as f: # or any binary file like '/bin/ls'
    in_bytes = f.read()
    print(in_bytes) # b'\n\x16\n\x04'
    hex_bytes = binascii.hexlify(in_bytes) 
    print(hex_bytes) # b'0a160a04' which is twice as long as in_bytes
    hex_str = hex_bytes.decode("ascii")
    print(hex_str) # 0a160a04

from the hex string "0a160a04" to can come back to the bytes with binascii.unhexlify("0a160a04") which gives back b'\n\x16\n\x04'

RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
7
import codecs
codecs.getencoder('hex_codec')(b'foo')[0]

works in Python 3.3 (so "hex_codec" instead of "hex").

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Richard Kiss
  • 71
  • 1
  • 2
6

it can been used the format specifier %x02 that format and output a hex value. For example:

>>> foo = b"tC\xfc}\x05i\x8d\x86\x05\xa5\xb4\xd3]Vd\x9cZ\x92~'6"
>>> res = ""
>>> for b in foo:
...     res += "%02x" % b
... 
>>> print(res)
7443fc7d05698d8605a5b4d35d56649c5a927e2736
Arg0s
  • 191
  • 1
  • 2
  • According to me, it is the best answer because it works with every Python version and does not need any import. Yet, I would better display hexa strings in upper case `res.upper()` – Bruno L. Apr 28 '20 at 18:24
  • No need for `upper()`!. You can control case by `x` or `X`: `'%02X' % 79 == '4F'` – vav May 19 '23 at 15:21
4

OK, the following answer is slightly beyond-scope if you only care about Python 3, but this question is the first Google hit even if you don't specify the Python version, so here's a way that works on both Python 2 and Python 3.

I'm also interpreting the question to be about converting bytes to the str type: that is, bytes-y on Python 2, and Unicode-y on Python 3.

Given that, the best approach I know is:

import six

bytes_to_hex_str = lambda b: ' '.join('%02x' % i for i in six.iterbytes(b))

The following assertion will be true for either Python 2 or Python 3, assuming you haven't activated the unicode_literals future in Python 2:

assert bytes_to_hex_str(b'jkl') == '6a 6b 6c'

(Or you can use ''.join() to omit the space between the bytes, etc.)

Peter
  • 349
  • 2
  • 7
  • I've found this useful when supporting Python 3.7, which does _not_ have a `sep` arg in `bytes.hex`. At time of writing, 3.7 is still a supported version. – theY4Kman Mar 02 '23 at 17:11
1

If you want to convert b'\x61' to 97 or '0x61', you can try this:

[python3.5]
>>>from struct import *
>>>temp=unpack('B',b'\x61')[0] ## convert bytes to unsigned int
97
>>>hex(temp) ##convert int to string which is hexadecimal expression
'0x61'

Reference:https://docs.python.org/3.5/library/struct.html

hao li
  • 367
  • 2
  • 13