Nabin Choudhary
Nabin Choudhary

Reputation: 13

Using Python, how to find XOR of two strings having hexadecimal values

s1 = '0x54'

s2 = '0xa1'

How do we XOR s1 and s2 to get '0xf5' as output?

Upvotes: 0

Views: 72

Answers (3)

Yash Talaiche
Yash Talaiche

Reputation: 45

RUN:

print(hex(int(s1,16)^int(s2,16)))

Upvotes: 0

Popescu Daniel
Popescu Daniel

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

aydee45
aydee45

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

Related Questions