47
How can we XOR hexadecimal numbers in Python? For example, I want to XOR 'ABCD' and '12EF', the answer should be 'B922'.
I used the code below, but it gives the wrong results.
# xor two strings of different lengths
def strxor(a, b):
if len(a) > len(b):
return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join(["%s" % (ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
key = '12ef'
m1 = 'abcd'
print(strxor(key, m1))