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