convert char to byte representation

Scott David Daniels scott.daniels at acm.org
Mon Oct 10 14:30:40 EDT 2005


Philipp H. Mohr wrote:
> I am trying to xor the byte representation of every char in a string with
> its predecessor. But I don't know how to convert a char into its byte
> representation. 
ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the byte.

> e.g. everything between $ and * needs to be xor:
> 	$GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287,00,05,25,103,44*
> to get the checksum.

Probably you want a byte-array here, rather than going char-by-char.
Try:
     import array
     base = ('$GPGSV,3,1,10,06,79,187,39,30,59,098,'
             '40,25,51,287,00,05,25,103,44*')
     bytes = array.array('b', base[1 : -1])
     for i in reversed(range(len(bytes))):
	bytes[i] ^= bytes[i-1]
     result = bytes.tostring()

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list