2009-07-17, 03:03 AM
Fiel Wrote:Python does not allow strings or chars to be xor'd. You have to change them from strings to bytes, xor them, then convert back.
Code:>>> MyChar = "P"
>>> MyChar ^ 0xFF
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
MyChar ^ 0xFF
TypeError: unsupported operand type(s) for ^: 'str' and 'int'
>>> import struct
>>> MyChar = struct.unpack('b', MyChar)[0] #Essentially atoi(). 'b' means "signed byte"
>>> print(MyChar)
80
>>> MyChar ^ 0xFF
175
I don't know any language which allows the simple
str1 ^ str2-feat, nor the char ^ int, but in python it would be something like
Code:
import struct
def xor(str1, str2):
str1Lst = struct.unpack("%db" % len(str1), str1)
str2Lst = struct.unpack("%db" % len(str2), str2)
return struct.pack("%db" % len(str1), map (lambda x, y: x^y, str1Lst, str2Lst))Now, that would require the same size on str1 and str2, but repeating str2 would be rather easy to produce, I'd assume.
I could eventually modify logxor in lisp so it would work as the non-destructive-string-xor-function, but it would require stuff I've not worked with as of right now. Besides, there's really no need for it in Lisp, unless you send in data to the xor-function which you don't know whether's a number or a string. But then again, you could just check its type.

