views:

91

answers:

2

I am trying to .split() a hex string i.e. '\xff\x00' to get a list i.e. ['ff', '00']

This works if I split on a raw string literal i.e. r'\xff\x00' using .split('\\x') but not if I split on a hex string stored in a variable or returned from a function (which I presume is not a raw string)

How do I convert or at least 'cast' a stored/returned string as a raw string?

+5  A: 
x = '\xff\x00'
y = ['%02x' % ord(c) for c in x]
print y

Output:

['ff', '00']
Mark Byers
+1 for solving the root problem instead.
Amber
Wow, thanks, that works. It may take me a bit to realise why...
Brendan
This might help: `len(x) == 2`; `'ff' in x == False`
jcdyer
Also: `'%02x' % ord('k') == '6b'
jcdyer
Ah ok, so each hex value is a 'single' character. `ord()` returns the integer worth i.e. ff -> 255. The `x` in a Python string format renders the hex representation of an integer (sans the `\x`) and the `0` pads it with zeroes and the `2` specifies the width. See http://docs.python.org/library/stdtypes.html#string-formatting-operations
Brendan
Yes, that's correct.
Mark Byers
This is often useful when dealing with binary data which needs to be converted to hex... `x.encode("hex")` which prints `'ff00'`. Likewise `'ff00'.decode("hex")` is `'\xff\x00'`.
Nick Craig-Wood
Nick—That's probably worth posting as a separate answer. Good stuff.
jcdyer
A: 

Here is a solution in the spirit of the original question:

x = '\xff\x00'
eval("r"+repr(x)).split('\\x')

It will return the same thing as r'\xff\x00'.split('\\x'): ['', 'ff', '00'].

catwell
Although that works for this specific example, it fails for many other strings. For example, x = '\x20\x20' gives [' '] instead of ['20', '20'].
Mark Byers
Yes, it will work only for non-printable characters so it's not really a good solution.
catwell