Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\x3C\x01" in python coding?

John Machin sjmachin at lexicon.net
Sat May 24 19:53:39 EDT 2008


Sebastian 'lunar' Wiesner wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> [ zxo102 <zxo102 at gmail.com> ]
> 
>>    how  to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
>> "\xED\x6F\x3C\x01" in python coding?
>> When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
>> the error information : invalid \x escape.
> 
> [1]--> 'ED6F3C01'.decode('hex')
> Out[1]: '\xedo<\x01'
> 

FWIW the 'hex' codec is just a big fat Python-coded wrapper around the 
routines in the C-coded binascii module; here's the evidence:

 >>> '123'.decode('hex')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "c:\python25\lib\encodings\hex_codec.py", line 42, in hex_decode
     output = binascii.a2b_hex(input)
TypeError: Odd-length string



More information about the Python-list mailing list