On Jan 20, 2006, at 7:37 PM, Sheila wrote: > --On January 20, 2006 7:24:47 PM -0800 Sheila King > <[EMAIL PROTECTED]> > wrote: > >> --On January 20, 2006 9:18:26 PM -0600 Silas Hundt >> <[EMAIL PROTECTED]> >> wrote: > ... >>> Receive input, cut that string up into individual characters (ALL >>> characters, including spaces), put them in order into a list, then >>> pull them out in order to convert them to a number. > ... >>>>> s = "This is a string." >>>>> list(s) >> ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', >> 'i', >> 'n', 'g', '.'] > > I'm not sure what numbers exactly you want to convert the > characters to, > but the one that springs to my mind are the ordinals for each > character. > So, if that's what you want, you could further do this: > >>>> chars = list(s) >>>> chars > ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', > 'i', 'n', > 'g', '.'] >>>> ords = map(ord, chars) >>>> ords > [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, > 110, 103, > 46]
You don't need that intermediate list, strings are iterable. If you want the big endian byte representation as a big number, you could do something like this: >>> chars = 'asdf' >>> sum([ord(c) << (8L * i) for i, c in enumerate(chars[::-1])]) 1634952294L >>> struct.unpack('>I', 'asdf')[0] # verify the result 1634952294L >>> chars = 'This is a string.' >>> sum([ord(c) << (8L * i) for i, c in enumerate(chars[::-1])]) 28722506059135649064412913099795503933230L You can also cheat by (ab)using built-in functionality in strange ways >>> long('This is a string.'.encode('hex'), 16) 28722506059135649064412913099795503933230L Little endian is the almost the same.. >>> chars = 'asdf' >>> sum([ord(c) << (8L * i) for i, c in enumerate(chars)]) 1717859169L >>> struct.unpack('<I', 'asdf')[0] # verify the result 1717859169L >>> chars = 'This is a string.' >>> sum([ord(c) << (8L * i) for i, c in enumerate(chars)]) 15790472653304512835830923089317093533780L >>> long('This is a string.'[::-1].encode('hex'), 16) 15790472653304512835830923089317093533780L You will need some kind of translation table if you want a value other than the ord, unless it's something like hex which has built-in support... -bob _______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig