[Tutor] Conversion question

Alan Gauld alan.gauld at btinternet.com
Tue May 5 11:37:27 CEST 2009


"Emile van Sebille" <emile at fenx.com> wrote

>> "414243440d0a"
>>
>> Is there a way in Python to say this is a string of HEX characters like 
>> Perl's pack?  Right now I have to take the string and add a \x to every 
>> two values i.e. \x41\x42...
>
> import binascii
> binascii.a2b_hex('41424344')

I hadn't come across binascii before, but it doesn't do what I expected:

>>> import binascii as b
>>> b.a2b_hex('414243440d0a')
'ABCD\r\n'
>>>

This appears to be converting it to a binary value then displaying that
binary value as an ascii string. I'm not sure what the value of that is
over struct or int? Can anyone enlighten me about why I'd ever want to
use this?

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list