Char to ASCII and back ?

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Apr 25 06:02:25 EDT 2003


On Fri, Apr 25, 2003 at 11:40:06AM +0200, Jean-Paul Roy wrote:
> How do you transform a char (or a one-char string) into the integer 
> ascii code and back ? Say to code a message.

You can use ord and chr for single characters:

    >>> ord('a')
    97
    >>> chr(97)
    'a'

But see also the struct module; it may be what you want for more complicated
packing and unpacking of bytes.

-Andrew.






More information about the Python-list mailing list