Convert to binary and convert back to strings

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Feb 22 16:18:10 EST 2007


On Thu, 22 Feb 2007 08:18:07 +0200, Hendrik van Rooyen wrote:

> I would xor each char in it with 'U' as a mild form of obfuscation...

I've often wished this would work.

>>> 'a' ^ 'U'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for ^: 'str' and 'str'

instead of the more verbose

>>> chr(ord('a') ^ ord('U'))
'4'


> Look at the array module to get things you can xor, or use ord() on
> each byte, and char()

Arrays don't support XOR any more than strings do. What's the advantage to
using the array module if you still have to jump through hoops to get it
to work?

''.join([chr(ord(c) ^ 85) for c in text])

is probably about as simple as you can get.



-- 
Steven.




More information about the Python-list mailing list