String to ASCII values

Max M maxm at mxm.dk
Thu Oct 9 16:58:48 EDT 2003


Benjamin Hepp wrote:

> Hello,
> 
> How do I convert a string like 'A' to its ASCII value(s) ?

The struct module is nifty, and most likely faster than ord()


#########

from struct import unpack

def toBytes(value):
     "Turns a string into a list of byte values"
     return unpack('%sB' % len(value), value)

print toBytes('ABCDEFG')



regards Max M





More information about the Python-list mailing list