# Byte-wise CRC16 Calculations

**URL:** https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249
**Category:** General
**Created:** [28 October 2019 18:35 UTC](https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249 "2019-10-28T18:35:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![WB\_Inst](https://avatars.discourse-cdn.com/v4/letter/w/f9ae1b/32.png) [@WB\_Inst](https://discourse.nodered.org/u/WB_Inst)
#### Post date: [28 October 2019 18:35 UTC](https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249/1 "2019-10-28T18:35:49Z")

</div>

Newbie looking for assistance.  
I am reading and writing commands (10 bytes) and messages (up to 100 bytes) hex bytes to and from a device. The packets are terminated with 2-byte CRC16. Because of the variable length packets the CRC16 is calculated byte-wise - the standard look-up table method doesn't give the correct results. I have tried numerous on-line calculators and none give the correct results.

I am able to extract and process received bytes from the msg.payload using function nodes but the algorithm to calculate the CRC for sending commands has me stumped.

I have the following from the C code:

It is CCITT CRC-16. The code is as follows:  
/\*\* @brief Add a single byte to a running calculation of a CCITT CRC-16.

- 

```
 Result is in global uCrcAccum.

```

- @param ch Byte to add.  
\*/  
void calc\_crc(char ch) {  
unsigned int shifter, flag;  
for (shifter = 0x80; shifter; shifter \>\>= 1) {  
flag = uCrcAccum & 0x8000;  
uCrcAccum \<\<= 1;  
uCrcAccum |= ((shifter & ch) ? 1 : 0);  
if (flag)  
uCrcAccum ^= 0x1021;  
}  
}

An example 10 byte message with 2 byte CRC: 0222000000000000000081B3

Any assistance would be appreciated. TNX.

---

<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: [28 October 2019 20:10 UTC](https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249/2 "2019-10-28T20:10:37Z")

</div>

Hi,

Did you search anywhere or try anything?

I ask because a quick search for "JavaScript CRC" turned up [this](https://github.com/donvercety/node-crc16/blob/master/crc16.js)

---

<div class="post-metadata">

### Author: ![WB\_Inst](https://avatars.discourse-cdn.com/v4/letter/w/f9ae1b/32.png) [@WB\_Inst](https://discourse.nodered.org/u/WB_Inst)
#### Post date: [28 October 2019 20:41 UTC](https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249/3 "2019-10-28T20:41:22Z")

</div>

Thanks. I did see that (and quite a few others) but it just didn't 'look' right to me. I'll look at it more closely.

---

<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: [28 October 2019 22:47 UTC](https://discourse.nodered.org/t/byte-wise-crc16-calculations/17249/4 "2019-10-28T22:47:41Z")

</div>

It might be worth using a handful of known data and known checksums and verify them [here](https://www.lammertbies.nl/comm/info/crc-calculation.html) before you rely on any one solution.
