I have a long Hex string that represents a series of values of different types. I wish to convert this Hex String into a byte array so that I can shift each value out and convert it into its proper data type.

share|improve this question

How does that hex string look like? – khachik Apr 13 '11 at 12:46
1  
There is a big difference between hexidecimal and hexadecimal - you might want to fix your title! – Steve Folly Apr 13 '11 at 13:34
@Steve Folly good catch – Richard Apr 13 '11 at 14:23
feedback

4 Answers

up vote 6 down vote accepted

Suppose your hex string is something like "deadbeef".

"deadbeef"

Convert it to a string:

result= hex_string.decode("hex")

Convert it to a byte array:

import array
result= array.array('B', hex_string.decode("hex"))

Convert it to a list of byte values:

result= map(ord, hex_string.decode("hex"))

or, possibly:

result= bytearray(hex_string.decode("hex"))

if on a recent enough Python.
_

However, it's possible that by “hex string” you just mean a string with unprintable characters.

"\x12\x45\x00AB"

In that case, use the options above ignoring the .decode("hex") part.

share|improve this answer
feedback

A good one liner is:

byte_list = map(ord, hex_string)

This will iterate over each char in the string and run it through the ord() function. Only tested on python 2.6, not too sure about 3.0+.

-Josh

share|improve this answer
perfect. Working on python 2.7 – Richard Apr 13 '11 at 13:06
Click the outline of the checkmark next to this answer if it's the right one! :) – jathanism Apr 13 '11 at 13:54
1  
This converts to a list of codepoints, not a bytearray. – Glenn Maynard Apr 13 '11 at 15:00
This doesn't convert hex - it converts each character of a string to an integer. For hex each pair of characters would represent a byte. You might as well just say byte_list = bytearray(hex_string) – Scott Griffiths Apr 13 '11 at 15:11
feedback

provided I understood correctly, you should look for binascii.unhexlify

import binascii
a='45222e'
s=binascii.unhexlify(a)
b=[ord(x) for x in s]
share|improve this answer
I agree that unhexlify is the most efficient way to go here, but would suggest that b = bytearray(s) would be a better than using ord. As Python has a built-in type just for arrays of bytes I'm surprised no one is using it – Scott Griffiths Apr 13 '11 at 15:03
feedback

You should be able to build a string holding the binary data using something like:

data = "fef0babe"
bits = ""
for x in xrange(0, len(data), 2)
  bits += chr(int(data[x:x+2], 16))

This is probably not the fastest way (many string appends), but quite simple using only core Python.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.