# Binary Check Code

Hi,
I have a device wich is connected to NodeRed via RS232 port, it has a proprietary communication protocol and a BCC (Binary Check Code). In the device manual I have this description of the BCC:

The checksum (also known as the binary check code, BCC) is generated by Exclusive-OR operation (XOR) all the bytes contained in the telegram (including STX and ETX). Since this algorithm will yield the same result if the sequence of bytes is altered, the positional index of each byte is added to it before the Exclusive-OR operation process. This yields a checksum that depends on the position of the bytes.

The function in pseudo-code:

``````Byte: unsigned character
Word: unsigned short
Pointer: array of Byte

Byte buildBCC (Pointer DataArray, Word NumberOfByte)
{
Word I = 0;
Byte BCC = 0;
repeat
{
BCC = BCC xor (I + DataArray[I]);
I = I + 1;
}
until (I equal NumberOfByte);
return BCC;
}
``````

I have made this function in NodeRed, but I need to ask more opinions:

``````var data = msg.payload;
var bcc = 0;
var str2 = ":";
var i;

for (i = 0; i < data.length; i++){
bcc = bcc^(i + data.charCodeAt(i));
str2 += ", " + (i + data.charCodeAt(i));
//bcc += data[i];
}

`bcc = bcc^(i + data[i])`