# Help on CRC-8 function

**URL:** <https://discourse.nodered.org/t/help-on-crc-8-function/34354>\
**Category:** Developing Nodes\
**Created:** [16 October 2020 15:14 UTC](https://discourse.nodered.org/t/help-on-crc-8-function/34354 "2020-10-16T15:14:07Z")\
**Posts on this page:** 1\
**Showing post:** 9

<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:** [17 October 2020 11:22 UTC](https://discourse.nodered.org/t/help-on-crc-8-function/34354/9 "2020-10-17T11:22:47Z")

</div>

> [@ej3359](#):
>
> 0x74 x052 0x7 0x2 0x3 0x15 0xd2  
> 0xd2 is the CRC

Assuming you meant `0x74 0x52 0x07 0x02 0x03 0x15 0xd2` has a CRC of `0xd2` then even that C code doesnt give `0xd2`

Assuming you meant `0x74 0x52 0x07 0x02 0x03 0x15` has a CRC of `0xd2` then even that C code doesnt give `0xd2` - you get `67` (or `0x43`)

C version...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31e975bf7a23adcbae1181435e05f956d0ba4f5e.png)  
Click [here](https://repl.it/@StephenMcLaughl/crc8) to see it in action

Additionally, my interpretation of that c code in JS function results in the same CRC

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

```auto
[{"id":"aa6441fb.6f381","type":"function","z":"56a2925d.72edac","name":"Calc CRC","func":"var crc= 0x85;\nvar poly= 0xFF;\n\nvar buf= msg.payload;\n\nfor (let i=0; i < buf.length; i++) {\n crc = (crc ^ (buf.readUInt8(i) & 0xff)) & 0xff\n for (let j=0; j < 8; j++) {\n if ((crc & 0x80)==0x80) {\n crc = ( ((crc<<1) & 0xff) ^ poly) & 0xff;\n } else {\n crc = ((crc & 0xff) << 1) & 0xff;\n }\n }\n}\n\nmsg.crc = crc;\nmsg.crchex = crc.toString(16);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":200,"wires":[["9cd60bb9.e21668"]]},{"id":"9d48507f.9343c","type":"inject","z":"56a2925d.72edac","name":"as hex string 745207020315","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hex string 745207020315","payload":"745207020315","payloadType":"str","x":620,"y":140,"wires":[["1bf1e4.0d454e1d"]]},{"id":"1bf1e4.0d454e1d","type":"function","z":"56a2925d.72edac","name":"to buffer","func":"msg.payload = Buffer.from(msg.payload,\"hex\")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":880,"y":140,"wires":[["aa6441fb.6f381"]]},{"id":"9cd60bb9.e21668","type":"debug","z":"56a2925d.72edac","name":"crc","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"crc","statusType":"msg","x":1050,"y":200,"wires":[]},{"id":"6634df59.7895f","type":"inject","z":"56a2925d.72edac","name":"as buffer [116 ,82 ,7 ,2 ,3 ,21]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"buffer [116 ,82 ,7 ,2 ,3 ,21]","payload":"[116 ,82 ,7 ,2 ,3 ,21]","payloadType":"bin","x":620,"y":240,"wires":[["aa6441fb.6f381"]]},{"id":"ac80a6c.4707958","type":"inject","z":"56a2925d.72edac","name":"as hex string 745207020315d2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"hex string 745207020315d2","payload":"745207020315d2","payloadType":"str","x":630,"y":100,"wires":[["1bf1e4.0d454e1d"]]},{"id":"f03c1bd1.83ecb8","type":"inject","z":"56a2925d.72edac","name":"as buffer [116 ,82 ,7 ,2 ,3 ,21 ,210]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"buffer [116 ,82 ,7 ,2 ,3 ,21 ,210]","payload":"[116 ,82 ,7 ,2 ,3 ,21, 210]","payloadType":"bin","x":640,"y":200,"wires":[["aa6441fb.6f381"]]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/help-on-crc-8-function/34354)._
