 # Trying to XOR a hexidecimal string

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!

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?

Put this in a FB...

``````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) {
}

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

``````

Output...

``````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
``````

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

1 Like

Yeah like my demo does exactly...

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

1 Like

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...

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

This is exactly what I needed, thanks a bunch!