Getting hex value of a character

Dave Brueck dave at pythonapocrypha.com
Thu Dec 12 13:42:51 EST 2002


On Thu, 12 Dec 2002, Dennis Reinhardt wrote:

> I am trying to print the hexadecimal value of a string.  In other words, the
> string "AB" would print as "4142".  I simply cannot get the formatting
> right.  Here is a test case:

Use the ord() function to get the ASCII value and '%X' to convert that to 
a hex string representation. To do it for all characters in a string:

''.join(['%X' % ord(c) for c in 'AB'])

-Dave





More information about the Python-list mailing list