0

s1 = '0x54'

s2 = '0xa1'

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

2
  • 0x54 ^ 0xa1 ?? Commented Sep 13, 2021 at 6:22
  • No they are hex values but their type is string Commented Sep 13, 2021 at 6:24

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0

RUN:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.