124

I have a long sequence of hex digits in a string, such as

000000000000484240FA063DE5D0B744ADBED63A81FAEA390000C8428640A43D5005BD44

only much longer, several kilobytes. Is there a builtin way to convert this to a bytes object in python 2.6/3?

recursive
  • 83,943
  • 34
  • 151
  • 241
  • 4
    Note that the answers below may look alike but they return different types of values. s.decode('hex') returns a str, as does unhexlify(s). bytearray.fromhex(s) returns a bytearray. Given the wording of this question, I think the big green checkmark should be on bytearray.fromhex(s), not on s.decode('hex'). – Paul Hoffman Dec 18 '13 at 02:04
  • 2
    Possible duplicate of [hexadecimal string to byte array in python](http://stackoverflow.com/questions/5649407/hexadecimal-string-to-byte-array-in-python) – Ciro Santilli OurBigBook.com May 17 '17 at 08:13
  • 2
    How can it be a duplicate of a question created 2 years later? – recursive May 17 '17 at 14:48
  • 1
    @CiroSantilli郝海东冠状病六四事件法轮功 A byte string isn't a byte array. https://stackoverflow.com/questions/1740696/bytes-vs-bytearray-in-python-2-6-and-3 – LarsH Nov 05 '20 at 11:30
  • 2
    @LarsH fair enough. @ recursive: date is not the main factor: https://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha – Ciro Santilli OurBigBook.com Nov 05 '20 at 11:42

5 Answers5

140
result = bytes.fromhex(some_hex_string)
vili
  • 1,840
  • 1
  • 14
  • 8
  • 3
    This seem like the most direct way to do what the original post is asking. Is there a reason this isn't the accepted answer? – Bash Jul 29 '20 at 00:58
  • 2
    The fromhex() method (of both bytes and bytearray) will also work when the hex numbers are separated by spaces. Very convenient! – Klaws Oct 16 '20 at 17:34
  • 2
    This really should be the accepted answer. The current accepted answer does not do what the question asked. It returns a mutable array of bytes, not a bytestring. – Mike Martin Oct 22 '20 at 19:14
  • 7
    @MikeMartin I accepted this answer, only 12 years after it was posted. – recursive Sep 25 '21 at 01:47
121

Works in Python 2.7 and higher including python3:

result = bytearray.fromhex('deadbeef')

Note: There seems to be a bug with the bytearray.fromhex() function in Python 2.6. The python.org documentation states that the function accepts a string as an argument, but when applied, the following error is thrown:

>>> bytearray.fromhex('B9 01EF')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fromhex() argument 1 must be unicode, not str`
Jens
  • 8,423
  • 9
  • 58
  • 78
Jim Garrison
  • 4,199
  • 4
  • 25
  • 39
  • 10
    And one additional step, I wanted a byte string (e.g. Python 3's b'\x04\xea[...]'), which you can get from a bytearray with `bytes(bytearray.fromhex('deadbeef'))` – berto Jan 15 '16 at 15:03
  • 6
    @berto: in that case there is a more direct route in the form of `binascii.unhexlify()`. – Martijn Pieters May 20 '16 at 11:57
  • 1
    Thanks, @MartijnPieters, I'll give that a shot – berto May 20 '16 at 12:26
  • 1
    This answer does not do what the question asked. It returns a mutable array of bytes, not a python bytestring. That's like returning an array of strings rather than a string. – Mike Martin Oct 22 '20 at 19:15
  • @MartijnPieters Seems like `bytes.fromhex()` is also more direct, and a bit less obscure / more parsimonious. https://docs.python.org/3/library/stdtypes.html#bytes.fromhex – LarsH Nov 05 '20 at 11:25
  • 2
    @LarsH: that method is not available in older Python 2 releases. That doesn't matter any more today, but it was an issue in 2016. – Martijn Pieters Nov 05 '20 at 11:40
  • @MartijnPieters I wondered if that was the case, but I didn't look far enough to find out. – LarsH Nov 05 '20 at 13:59
45

You can do this with the hex codec. ie:

>>> s='000000000000484240FA063DE5D0B744ADBED63A81FAEA390000C8428640A43D5005BD44'
>>> s.decode('hex')
'\x00\x00\x00\x00\x00\x00HB@\xfa\x06=\xe5\xd0\xb7D\xad\xbe\xd6:\x81\xfa\xea9\x00\x00\xc8B\x86@\xa4=P\x05\xbdD'
Brian
  • 116,865
  • 28
  • 107
  • 112
39

Try the binascii module

from binascii import unhexlify
b = unhexlify(myhexstr)
Scott Griffiths
  • 21,438
  • 8
  • 55
  • 85
Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
  • 11
    Two ways to do it in 2.x, three ways in 3.x. So much for "there's only one way to do it"... – technomalogical Jan 14 '09 at 18:58
  • 1
    Other two ways are more 'built-in' so I would actually use one of those. – Crescent Fresh Jan 14 '09 at 19:17
  • @technomalogical: your comment is irrelevant to the answer; perhaps you should delete it and change it into a post to comp.lang.python . – tzot Jan 15 '09 at 13:51
  • 1
    @technomalogical: I agree with ΤΖΩΤΖΙΟΥ. Also, you got it wrong. The correct phrase is: There should be one-- and preferably only one --*obvious* way to do it. – nosklo Jan 16 '09 at 11:40
  • 2
    Note that in Python 3.2 (whether by design or a bug I'm not sure) `unhexlify` now won't accept a string, but only bytes. Pretty silly really, but it means you'd need to use `b = unhexlify(bytes(myhexstr, 'utf-8'))` – Scott Griffiths May 29 '11 at 16:45
2
import binascii

binascii.a2b_hex(hex_string)

Thats the way I did it.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
JustPlayin
  • 89
  • 11