Search code examples
pythonhexxor

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?


Solution

  • 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