0

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
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Amir Ali
  • 225
  • 1
  • 9
  • 1
    Your hexarr is a string, so the first `f` is just a character with the code 102 (hex 66). It has nothing to do with the hex digit `f` (=15). – georg Oct 30 '19 at 10:21
  • 1
    The problem is that you're not converting to the bases you're expecting to convert. What you call `binaryData` and `hexData` are really just decimal number that appear to be in the base you expected. – Federico klez Culloca Oct 30 '19 at 10:22
  • if you get binary value of `3` which is `11` and get XOR afterwards. as @georg said there is a fundamental mistake over there. but try XORing `1100110 ^ 11` = `1100101` – sertsedat Oct 30 '19 at 10:25
  • so how to convert my first 'f' into binary and then XOR on it ??? @FedericoklezCulloca – Amir Ali Oct 30 '19 at 10:32
  • @sertsedat its OK for manual solution.. but i must have to read from variables, convert it to binary then perform XOR by mask of 1,3,7,15, 255.... – Amir Ali Oct 30 '19 at 10:36
  • 1
    @AmirAli you don't need to convert to binary. A number is a number. 3 decimal and 11 binary are the same number. You just need to show the binary representation when you `console.log` it, so `binayrData = hexarr.charCodeAt(0); console.log("binaryData: ", binayrData.toString(2)); console.log("binaryData^3: ", (binayrData^3).toString(2));` – Federico klez Culloca Oct 30 '19 at 10:39

1 Answers1

2

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));
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • thanks for you answer.. its working .. But not useful if i want to XOR with 255 (means 11111111) ... I think there must be some signed / unsigned conversion.. If yes, then how i could do the toggle all 8 bits (means XOR with 255) .... – Amir Ali Oct 30 '19 at 11:05
  • If that's what you need (which isn't what you asked) see [this other question](https://stackoverflow.com/questions/42450510/invert-unsigned-arbitrary-binary-bits-in-javascript) – Federico klez Culloca Oct 30 '19 at 11:19
  • thank for your time. I can XOR value, but still i could not assign back into its original array. After XORing, same values i.e. 102 ("f") is existed, the XORed value 101 ("e") is not reflected back to hexarr ... see following code var hexarr = 'f86b8204'; console.log("array before is : ", hexarr.charCodeAt(0)); d = hexarr.charCodeAt(0)^3; console.log("d is : ",d) // 101 printed hexarr[0] = d; console.log("array after is : ", hexarr.charCodeAt(0)); // 102 – Amir Ali Oct 30 '19 at 12:17
  • `hexarr.charCodeAt(0)` does not refer to the character in the string directly. It's a copy of the character, so you don't change it in the string. Put it in a variable and replace that variable in the original string (it's a string, not an array; you better learn the correct terms so others understand you). See [this other question](https://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript) on how to replace characters. – Federico klez Culloca Oct 30 '19 at 12:35
  • Having said that, you're asking a lot of questions that don't directly pertain your original one. You should create other questions if you need different things than what you originally asked. Also, it looks like whatever material you're using to learn JS is severely lacking. I suggest you take a look at [MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript) – Federico klez Culloca Oct 30 '19 at 12:37