[Tutor] converting to Ascii from hex

lonetwin lonetwin <lonetwin@yahoo.com>
Fri, 8 Feb 2002 14:14:17 +0530 (IST)


Hi everybody,
    For a program I'm working on I get data in ASCII hex digit
characters (0-9, A-F), with two sequential ASCII hex digits corresponding
to the hex value of each data byte. Eg: (because I know what I just said
makes no sense :)), an ASCII "A" data character is sent as the two ASCII
characters "41" (ie: 0x41 == 65 == ord('A')
    Now, the problem is I need to intepret this, what follows below is
what I could hack up, but is kinda ugly. Could someone gimme
something better ??

intepreter session:
=========================================================
>>> p ="""416268696A6565740A353435303134392870756E65290D
... 35353130363639286F666629"""
>>> s = [ chr(int(p[x:x+2], 16)) for x in range(0, len(p), 2) ]
>>> s
['A', 'b', 'h', 'i', 'j', 'e', 'e', 't', '\n', '5', '4', '5', '0', '1',
'4', '9', '(', 'p', 'u', 'n', 'e', ')', '\r', '5', '5', '1', '0', '6',
'6', '9', '(', 'o', 'f', 'f', ')']
>>>
=========================================================

Peace
Steve