Reputation: 225
When I XOR binary data with decimal value, gives wrong results.
Considered my following program:
var hexarr = 'f86b8204';
binayrData = hexarr.charCodeAt(0).toString(2);
decimalData = hexarr.charCodeAt(0);
hexData = hexarr.charCodeAt(0).toString(16);
console.log("binaryData: ", binayrData);
console.log("binaryData^3: ", binayrData ^ 3);
console.log("decimalData : ", decimalData);
console.log("decimalData^3 : ", decimalData ^ 3);
console.log("hexData: ", hexData);
console.log("hexData^3: ", hexData ^ 3);
and here is output
binaryData: 1100110
binaryData^3: 1100109
decimalData : 102
decimalData^3 : 101
hexData: 66
hexData^3: 65
Upvotes: 0
Views: 140
Reputation: 27149
When you think you're converting your numbers to a different base, you're actually creating strings that are representations of your number in those bases. When you then try to XOR them, JS converts those strings to numbers, but has no idea what bases they're in and treats them as decimals.
What you actually want to do is to push the base conversion to the point when you display your data.
var hexarr = 'f86b8204';
binaryData = hexarr.charCodeAt(0);
console.log("binaryData: ", binaryData.toString(2));
console.log("binaryData^3: ", (binaryData ^ 3).toString(2));
console.log("decimalData : ", binaryData.toString(10));
console.log("decimalData^3 : ", (binaryData^ 3).toString(10));
console.log("hexData: ", binaryData.toString(16));
console.log("hexData^3: ", (binaryData ^ 3).toString(16));
Upvotes: 2