How to use Excel to calculate bitwise XOR in hexadecimal format?

Anonymoose picture Anonymoose · Oct 9, 2015 · Viewed 15.8k times · Source

I have two cells with a string of bits:

1747F6001E00DB2XXXXX28FE5257645C
and
1C6262C8DBF510F655XXXXXA3BDA58AC

I want to XOR the two together so that the result would be something like 0B2594C8C5F5CXXXXX190014698D3CF0.

I know that I need to use a bitwise XOR operation in Excel, as bitwise XOR calculators work online, but they give me the option to choose between hexadecimal and binary input data. BITXOR should work, but it does not work for hexadecimal inputs.

Answer

Axel Richter picture Axel Richter · Oct 9, 2015

An approach for two hex-strings in equal length.

Sub approach()

 s1 = "1747F6001E00DB266F5728FE5257645C"
 s2 = "1C6262C8DBF510F6554E28EA3BDA58AC"

 If Len(s1) <> Len(s2) Then Exit Sub

 sRes = ""
 For i = 1 To Len(s1)
  vRes = Val("&H" & Mid(s1, i, 1)) Xor Val("&H" & Mid(s2, i, 1))
  sRes = sRes & Hex(vRes)
 Next

 Debug.Print sRes

End Sub

XOR each digit of the two hex-values as there is no need for an carry since it is not an addition.

Btw. BITXOR will only work up to 2^48-1. You have a value range up to 16^32-1.