Python – How to XOR two strings that contain hex numbers in python

hexpythonstringxor

I have looked for an answer online, but none of them seem to solve my problem in my way (I know, I'm picky :D).

Here's the deal: I am using the string type to store two hex numbers, because the default integer type in python is not long enough for my purposes. For example like this:

S1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e"
S2 = "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f"

The point is, that these are supposed to be NUMBERS, but they are stored in a string. What I want to do, is to treat these two strings as numbers, perform a bitwise XOR on the two and then get an output in a similar form – a hex number stored in a string.

I am rather new to programming and even newer to python, so I couldn't figure out a way to do this. I am not just looking for a script, I would also like to understand how it works, so please be thorough with your explanation, as I am quite the noob :D.

Best Answer

Python can hold your values as numbers.

See this for proof,

>>> hex(int(S1, 16))[2:-1] == S1
True

I'm simply adjusting the string, removing '0x' from the beginning and L from the end.

For your answer all you need to do is,

hex(int(S1, 16) ^ int(S2, 16))