[Tutor] Converting ascii string to hex?

Terry Carroll carroll at tjc.com
Tue Aug 12 22:11:42 EDT 2003


Is there a straightforward way to convert an ascii string to printable 
hexadecimal?

I've kludged up this, but there has to be a cleaner way:

>>> def asctohex(string_in):
...     a=""
...     for x in string_in:
...         a = a + (hex(ord(x)))[2:4]
...     return(a)
...
>>> print asctohex("a 1")
612031
>>>

-- 
Terry Carroll        |   "I say to you that the VCR is to the American
Santa Clara, CA      |   film producer and the American public as the 
carroll at tjc.com      |   Boston strangler is to the woman home alone."  
                     |       Jack Valenti, MPAA President
Modell delendus est  |       Testimony before Congress, 1982




More information about the Tutor mailing list