9

H, I'm trying to XOR two strings (which should become hex first) in Python. I know one way will work:

def xor_two_str(str1, str2):
    return hex(int(str1,16) ^ int(str2,16))

But I tried sth like this:

def change_to_be_hex(str):
    return hex(int(str,base=16))
def xor_two_str(str1,str2):
    a = change_to_be_hex(str1)
    b = change_to_be_hex(str2)
    return hex(a ^ b)
print xor_two_str("12ef","abcd")

This will return TypeError: ^ shouldn't be used between str, str. I don't know why.

And also this function won't work:

bcd = change_to_be_hex("12ef")
def increment_hex(hex_n):
   return hex_n + 1
result = increment_hex(bcd)
print result

The error message is : TypeError: cannot concatenate 'str' and 'int' objects I feel this is so strange:(

Thank you!

8
  • 1
    @n1c9 he means byte xor operator, not power operator
    – xvan
    Commented Mar 27, 2016 at 1:33
  • @n1c9 Umm... Yes, it is
    – jDo
    Commented Mar 27, 2016 at 1:35
  • i stand corrected!
    – n1c9
    Commented Mar 27, 2016 at 1:40
  • So you know how to XOR numbers represented as hex strings and you know that you can't use ^ on strings. What's your question then? Commented Mar 27, 2016 at 1:43
  • OP, don't you want to XOR the numerical value of every character in string1 with the value of the character at the same position in string2? (modulo length to wrap around in case they're not the same length). Why bother with the hex conversion?
    – jDo
    Commented Mar 27, 2016 at 1:49

3 Answers 3

12

Hi, The following function is returning the result of hex() which returns a string.

def change_to_be_hex(s):
    return hex(int(s,base=16))

You should use the ^ operator on integers.

def change_to_be_hex(s):
    return int(s,base=16)
    
def xor_two_str(str1,str2):
    a = change_to_be_hex(str1)
    b = change_to_be_hex(str2)
    return hex(a ^ b)
print(xor_two_str("12ef","abcd"))

I'm not sure though that's the result you're looking for. If you want to XOR two strings, it means you want to XOR each character of one string with the character of the other string. You should then XOR ord() value of each char or str1 with ord() value of each char of str2.

def xor_two_str(a,b):
    xored = []
    for i in range(max(len(a), len(b))):
        xored_value = ord(a[i%len(a)]) ^ ord(b[i%len(b)])
        xored.append(hex(xored_value)[2:])
    return ''.join(xored)
    
print(xor_two_str("12ef","abcd"))

Or in one line :

def xor_two_str(a,b):
    return ''.join([hex(ord(a[i%len(a)]) ^ ord(b[i%(len(b))]))[2:] for i in range(max(len(a), len(b)))])

print(xor_two_str("12ef","abcd"))
2

hex returns a string, so you're trying to xor two strings.

def change_to_be_hex(s):
   return int(s,base=16)

Should fix this.

1

when you initially return hex, like in change_to_be_hex, you explicitly convert it to int. you need to do that throughout your code to add something to it - so, change increment_hex to:

return (int(hex_n) + 1)
2
  • 2
    ^ IS recognized, it's the bitwise xoring operation.
    – pixis
    Commented Mar 27, 2016 at 1:34
  • 1
    Have you tried 4 ^ 5? What do you know? no error. Not only is ^ recognized, it does something quite similar to what the OP wants.
    – zondo
    Commented Mar 27, 2016 at 1:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.