convert char to byte representation

Rick Wotnaz desparn at wtf.com
Mon Oct 10 14:46:19 EDT 2005


Scott David Daniels <scott.daniels at acm.org> wrote in
news:434ab2f6$1 at nntp0.pdx.net: 

> 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
> 

What is the byte representation of 287?

-- 
rzed



More information about the Python-list mailing list