ASCII to HEX conversion

Chris Liechti cliechti at gmx.net
Thu Apr 11 18:20:32 EDT 2002


leisinmr at purdue.edu (Matt) wrote in 
news:34e98ec2.0204111415.504d246f at posting.google.com:

> I need to write a program that converts ASCII test into the
> hexidecimal equivalent.  It would help if it could convert to binary,
> too.
> 
> Example:
> 
> ASCII = "A"
> HEX = 41
> 
> I tried using the hex() function, but it does not work.  If anyone
> could tell me how to do this without making a lookup table of all the
> ASCII values.

use the ord function to get the ascii code of a character. you can convert 
that number to any format you want. 

>>> [hex(ord(x)) for x in "ABCDE"]
>>> ["%02X" % (ord(x)) for x in "ABCDE"]

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list