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.
feedback
|
Suppose your hex string is something like "deadbeef"Convert it to a string:
Convert it to a byte array:
Convert it to a list of byte values:
or, possibly:
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 | ||||
feedback
|
A good one liner is:
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 | |||||||||||
feedback
|
provided I understood correctly, you should look for binascii.unhexlify
| |||
feedback
|
You should be able to build a string holding the binary data using something like:
This is probably not the fastest way (many string appends), but quite simple using only core Python. | |||
feedback
|