Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing data within bytes...
#3
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.
Reply


Messages In This Thread
Changing data within bytes... - by Nikkey - 2009-07-17, 02:29 AM
Changing data within bytes... - by Fiel - 2009-07-17, 02:43 AM
Changing data within bytes... - by Nikkey - 2009-07-17, 03:03 AM
Changing data within bytes... - by AngelSL - 2009-07-17, 04:03 AM
Changing data within bytes... - by Nikkey - 2009-07-17, 04:43 AM
Changing data within bytes... - by Spaz - 2009-07-17, 04:11 PM
Changing data within bytes... - by Dusk - 2009-07-17, 04:35 PM
Changing data within bytes... - by Kortestanov - 2009-07-17, 05:20 PM
Changing data within bytes... - by Nikkey - 2009-07-17, 06:51 PM
Changing data within bytes... - by Spaz - 2009-07-17, 07:36 PM
Changing data within bytes... - by Fiel - 2009-07-17, 07:42 PM
Changing data within bytes... - by Nikkey - 2009-07-17, 08:37 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)