# \[update\]Need help with CRC16

**URL:** <https://discourse.nodered.org/t/update-need-help-with-crc16/71891>\
**Category:** General\
**Created:** [8 December 2022 20:08 UTC](https://discourse.nodered.org/t/update-need-help-with-crc16/71891 "2022-12-08T20:08:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [8 December 2022 21:20 UTC](https://discourse.nodered.org/t/update-need-help-with-crc16/71891/2 "2022-12-08T21:20:31Z")

</div>

Adapted from here: [Modbus crc16 calculation function error syntax - #6 by Steve-Mcl](https://discourse.nodered.org/t/modbus-crc16-calculation-function-error-syntax/68657/6)

Function...

```auto
 function crc16(str) {
    const buf = Buffer.from(str)
    let crc = 0xFFFF;
    for (let i = 0; i < buf.length; i++) {
        crc = crc ^ buf[i];
        for (let j = 0; j < 8; j++) {
            const temp = crc & 0x01;
            crc >>= 0x01;
            if (temp == 0x01) {
                crc ^= 0xA001;
            }
        }
    }
    return crc;
}

```

Proof...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af6a1b3fe3c5c9aaa7a1afc2a863517e381b974e.png)

---

_[View the full topic](https://discourse.nodered.org/t/update-need-help-with-crc16/71891)._
