[Tutor] ASCII characters
Kent Johnson
kent37 at tds.net
Tue May 24 20:27:10 CEST 2005
D. Hartley wrote:
> I have a question: what is the "opposite" of hex()? (i.e., like ord
> and chr). If I have
>
> '0x73', how can I get back to 115 or s?
I don't know a really clean way to do this because '0x73' is not a legal input value for int().
The simplest way is to use eval():
>>> eval('0x73')
115
but eval() is a security hole so if you are not 100% sure of where your data is coming from then
this is probably a better solution (it strips off the '0x' then tells int() to convert base 16):
>>> int('0x73'[2:], 16)
115
To go from the string '115' to the integer 115 you can use int() directly:
>>> int('115')
115
To get back to a string use chr():
>>> chr(115)
's'
All of these functions are documented here:
http://docs.python.org/lib/built-in-funcs.html
Kent
More information about the Tutor
mailing list