# Binary Check Code

**URL:** <https://discourse.nodered.org/t/binary-check-code/17324>\
**Category:** General\
**Created:** [30 October 2019 12:23 UTC](https://discourse.nodered.org/t/binary-check-code/17324 "2019-10-30T12:23:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iridiu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/iridiu/32/1213_2.png) [@iridiu](https://discourse.nodered.org/u/iridiu)\
**Post date:** [30 October 2019 12:23 UTC](https://discourse.nodered.org/t/binary-check-code/17324/1 "2019-10-30T12:23:01Z")

</div>

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:

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

```auto
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];
}

msg.payload = bcc;
msg.str2 = str2;

return msg;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 October 2019 16:07 UTC](https://discourse.nodered.org/t/binary-check-code/17324/2 "2019-10-30T16:07:46Z")

</div>

> [@iridiu](#):
>
> bcc = bcc^(i + data.charCodeAt(i));

I think that should probably be  
`bcc = bcc^(i + data[i])`
