135

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?

5
  • 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'). Commented Dec 18, 2013 at 2:04
  • 2
    Possible duplicate of hexadecimal string to byte array in python Commented May 17, 2017 at 8:13
  • 3
    How can it be a duplicate of a question created 2 years later? Commented May 17, 2017 at 14:48
  • 1
    @CiroSantilli郝海东冠状病六四事件法轮功 A byte string isn't a byte array. stackoverflow.com/questions/1740696/… Commented Nov 5, 2020 at 11:30
  • 2
    @LarsH fair enough. @ recursive: date is not the main factor: meta.stackexchange.com/questions/147643/… Commented Nov 5, 2020 at 11:42

5 Answers 5

158
result = bytes.fromhex(some_hex_string)
Sign up to request clarification or add additional context in comments.

1 Comment

The fromhex() method (of both bytes and bytearray) will also work when the hex numbers are separated by spaces. Very convenient!
122

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`

6 Comments

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: in that case there is a more direct route in the form of binascii.unhexlify().
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.
@MartijnPieters Seems like bytes.fromhex() is also more direct, and a bit less obscure / more parsimonious. docs.python.org/3/library/stdtypes.html#bytes.fromhex
@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.
|
46

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'

1 Comment

codecs.decode('0a0a0a', 'hex_codec') should work for 2.x and 3.x :-)
39

Try the binascii module

from binascii import unhexlify
b = unhexlify(myhexstr)

5 Comments

Two ways to do it in 2.x, three ways in 3.x. So much for "there's only one way to do it"...
Other two ways are more 'built-in' so I would actually use one of those.
@technomalogical: your comment is irrelevant to the answer; perhaps you should delete it and change it into a post to comp.lang.python .
@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.
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'))
2
import binascii

binascii.a2b_hex(hex_string)

Thats the way I did it.

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.