How to XOR two hex strings in Python 3?
11,935
Strings in Python 3 are unicode objects and so a string of hexadecimal characters does not correspond to the binary representation of the integer in memory (which you need to use XOR).
With this in mind, you could interpret the strings as base-16 integers first, XOR them, and convert the resulting integer back to a hex string:
>>> hex(int(string1, 16) ^ int(string2, 16))
'0xdd42218c5358e7d2'
Related videos on Youtube
Author by
John Smith
Updated on June 14, 2022Comments
-
John Smith over 1 year
I have two strings stored in 2 separate files,
string1="95274DE03C78B0BD"
andstring2="48656c6c6f20576f"
.How can I do bitwise XOR them in Python 3? I expect to get
DD42218C5358E7D2
as my result. Please note that I don't want toord()
the strings, my strings are already in hex. -
Jonas over 3 yearsuse: xored[2:] to get rid of the "0x" in the begining.