XOR two bytes?

Steve Holden sholden at holdenweb.com
Sat Aug 4 19:20:13 EDT 2001


[posted & mailed]

"Tim Rowe" <digitig at cix.co.uk> wrote in message
news:memo.20010805001357.60291M at digitig.compulink.co.uk...
> In article <3B6C5985.EA285A07 at pacbell.net>, sntial at pacbell.net (Jeanne)
> wrote:
>
> > Does anyone know if there is a function to quickly Xor two bytes
> > together?
> > I have been searching in the docs, but havent' been able to find it.
> > Please cc: my cris_paltenghe at yahoo.com address as I don't hang out in
> > this newsgroup often.  Thanks for your help! Cris
>
> Do you mean '^'?

If the bytes are single-character strings then the ord() and chr() builtin
functions complete the picture.

>>> def xorchars(c1, c2):
...  return chr(ord(c1) ^ ord(c2))
...
>>> xorchars("A", "a")
' '
>>> xorchars("A", "c")
'"'
>>>

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list