thedethfox thedethfox - 9 months ago 267
Javascript Question

XOR of two hex strings in JavaScript

var hex1 = "B1C85C061C98E713DEF0E2EDDDDB432738674C9F8962F09B75E943D55F9FB39F";
var hex2 = "121B0D3327A21B8048FC7CA6FD07AACC0D8DF59B99DB098686696573E3686E6C";

var result = hex1 ^ hex2; //XOR the values

console.log(result); // outputs: 0 which does not sound good.

Any ideas how to perform XOR operations on hex values?


Bitwise operations in JavaScript only work on numeric values.

You should parseInt(hexString, 16) your hex string before. Specifically in your case this wouldn't work because your hex is to big for a number. You would have to create your own customized XOR function.

Take a look at this link: How to convert hex string into a bytes array, and a bytes array in the hex string?

The resulting bytearray will be ellegible for a manual XOR. Byte by byte. Maybe this will help: Java XOR over two arrays.