"Steven D'Aprano" <[EMAIL PROTECTED]> wrote:
> 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' you are not alone in this - to do something simple like calculating a BCC on a string, or a checksum like at the end of a line in an Intel hex file is a bit of a pain in Python. > > > > 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? I think you will have less function calls, but I may be wrong: s = 'some string that needs a bcc appended' ar = array.array('B',s) bcc = 0 for x in ar[:]: bcc ^= x ar.append(bcc) s=ar.tostring() > > ''.join([chr(ord(c) ^ 85) for c in text]) > > is probably about as simple as you can get. > This is nice and compact. It would be very nice if you could just use a single char string like an int and apply the operators to it - the Python way seems so left- handed - make it an int, do the work, make it back into a string - and all the time we are working on essentially a one byte value... - Hendrik -- http://mail.python.org/mailman/listinfo/python-list