# Trying to XOR a hexidecimal string

**URL:** <https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053>\
**Category:** General\
**Created:** [3 September 2019 11:08 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053 "2019-09-03T11:08:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stylo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stylo/32/14221_2.png) [@Stylo](https://discourse.nodered.org/u/Stylo)\
**Post date:** [3 September 2019 11:08 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/1 "2019-09-03T11:08:41Z")

</div>

Hi there,

I'm working on a project in which I'm sending and receiving hexadecimal message strings from a bluetooth gateway via MQTT.

My problem is; I need to XOR each hexidecimal string to make sure it's correct when pushing messages.

For example, if my message is "03000E010004" I need to XOR it like '03'/'00', '00'/'0E', etc.

Does anyone know how I would do this with hexidecimal messages? I know it's possible with binary but I haven't been able to find a reliable way to convert these messages to binary and XOR them without it getting extremely messy.

Thanks!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 September 2019 14:01 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/2 "2019-09-03T14:01:51Z")

</div>

> [@Stylo](#):
>
> For example, if my message is "03000E010004" I need to XOR it like '03'/'00', '00'/'0E', etc.

Are you wanting to XOR 03 with 00, then 00 with 0E, then 0E with 01 etc?

If so, what do you want to happen with the 04 at the end?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 September 2019 14:33 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/3 "2019-09-03T14:33:12Z")

</div>

Put this in a FB...

```auto
var str = msg.payload;
if( (str.length % 2) !== 0){
  node.error("String length must be Even");
  return;
}

var numbers = []
for(var i = 0; i < str.length; i+=2){
  var num = str.slice(i,i+2)
  numbers.push(parseInt(num,16))
}

function doXOR(total, num, idx) {
  return total ^ num;
}

var result = numbers.reduce(doXOR)
msg.payload = result;
return msg;

```

Output...

```auto
numbers [83, 242, 231, 81, 68, 4]
idx, this num, running xor 1 242 83
idx, this num, running xor 2 231 161
idx, this num, running xor 3 81 70
idx, this num, running xor 4 68 23
idx, this num, running xor 5 4 83
Result (decimal) = 87
Result (hexadecimal) = 0x57

```

[Click here to see it in action](https://repl.it/@StephenMcLaughl/jsxor)

---

<div class="post-metadata">

**Author:** ![Stylo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stylo/32/14221_2.png) [@Stylo](https://discourse.nodered.org/u/Stylo)\
**Post date:** [4 September 2019 13:59 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/4 "2019-09-04T13:59:42Z")

</div>

Sorry, let me clarify.

I'd want to XOR 03 and 00, then XOR the result of that with 0E, so on and so forth.

E.g. a message of 03010EFF  
03 & 01 = 02,  
02 & 0E = 0C,  
0C & FF = F3

Therefore the output would be F3

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 September 2019 19:46 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/5 "2019-09-04T19:46:18Z")

</div>

Yeah like my demo does exactly...

 ![Screenshot_20190904-204503](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/49a4aed0c50b7e5ec6ca8f4bd0eef8f48207d059.jpeg)

Look at the top line, changed it to your data. Look at the result at the end.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 September 2019 19:50 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/6 "2019-09-04T19:50:49Z")

</div>

Did you even try the code I wrote for you?

If you REALLY want the hex string (and not the actual XOR result) then change the 2nd last line to...

```auto
msg.payload = result.toString(16);

```

---

<div class="post-metadata">

**Author:** ![Stylo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stylo/32/14221_2.png) [@Stylo](https://discourse.nodered.org/u/Stylo)\
**Post date:** [16 September 2019 13:56 UTC](https://discourse.nodered.org/t/trying-to-xor-a-hexidecimal-string/15053/7 "2019-09-16T13:56:18Z")

</div>

This is exactly what I needed, thanks a bunch!
