Reputation: 13
s1 = '0x54'
s2 = '0xa1'
How do we XOR s1 and s2 to get '0xf5' as output?
Upvotes: 0
Views: 72
Reputation: 24
s1 = '0x54'
s2 = '0xa1'
def XOR(s1, s2):
return "0x" + "{:x}".format(int(s1[2:], 16)^int(s2[2:], 16))
XOR(s1,s2)
That should work
Upvotes: 0
Reputation: 516
First convert them to numeric values:
n1 = int(s1, 0)
n2 = int(s2, 0)
Now compute the XOR
of the values using the ^
operator, and convert to hex
format:
result = hex(n1 ^ n2)
Upvotes: 3