# Creating CRC16 xModem Hex Value

**URL:** <https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981>\
**Category:** General\
**Created:** [12 October 2023 19:33 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981 "2023-10-12T19:33:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [12 October 2023 19:33 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/1 "2023-10-12T19:33:33Z")

</div>

I'm new here and rusty on my Java sorry if this sounds silly and before you say I've tried the other postings and I couldn't get them to work. What we're need to do is to send an ASCII base64 value from a JSON object string, we're getting that for a PNG, and I need to get the CRC16/Xmodem value of it. I tried npm i crc16-xmodem but the hex value I'm getting back from debug does not equal.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 October 2023 07:44 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/2 "2023-10-13T07:44:02Z")

</div>

@michaelbusry welcome to the forum! First off it is JavaScript not Java they are two different things (grin)

Can you give some background info:

1. what version of Node-RED, and node.js are you running (see node-redstarxtup log)
2. what platform are you running NR on?

So you have something running so can you create a test flow demonstrating the problem. If you add a `debug` node to the output of what ever is sending you the json, you could put that in an `inject` node in the test flow. That way we can run your test flow to see what is happening.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [13 October 2023 08:53 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/3 "2023-10-13T08:53:31Z")

</div>

Did you try this: [CRC-32 can't get the correct check sum - any help appreciated - #5 by Steve-Mcl](https://discourse.nodered.org/t/crc-32-cant-get-the-correct-check-sum-any-help-appreciated/77627/5)

Forget for a second the title states "CRC-32" - under the hood it is a universal solution.

Example:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7cfb120383b956835a22c730e486dc71c62fd91.png)

```auto
[{"id":"e2beeb574d982801","type":"inject","z":"166e28321a352483","name":"JSON encoded base64","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"eyAia2V5IjogInZhbHVlIiB9","payloadType":"str","x":420,"y":240,"wires":[["4bf21b40a1445e15"]]},{"id":"4bf21b40a1445e15","type":"function","z":"166e28321a352483","name":"easy-crc","func":"//See https://www.npmjs.com/package/easy-crc?activeTab=readme for list of supported algorithms\n\nconst { crc16 } = easyCrc;\nconst data = Buffer.from(msg.payload, 'base64'); // if base64 encoded string\nmsg.payload = crc16('XMODEM', data)\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[{"var":"easyCrc","module":"easy-crc"}],"x":600,"y":240,"wires":[["25cd665db5eaa0d9"]]},{"id":"25cd665db5eaa0d9","type":"debug","z":"166e28321a352483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":240,"wires":[]},{"id":"38bebaafa0df23fd","type":"inject","z":"166e28321a352483","name":"JSON string","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"key\": \"value\" }","payloadType":"str","x":390,"y":320,"wires":[["116fd588198b5f41"]]},{"id":"116fd588198b5f41","type":"function","z":"166e28321a352483","name":"easy-crc","func":"//See https://www.npmjs.com/package/easy-crc?activeTab=readme for list of supported algorithms\n\nconst { crc16 } = easyCrc;\nconst data = Buffer.from(msg.payload, 'utf-8'); // if JSON string\nmsg.payload = crc16('XMODEM', data)\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[{"var":"easyCrc","module":"easy-crc"}],"x":600,"y":320,"wires":[["bb86f44366cee582"]]},{"id":"bb86f44366cee582","type":"debug","z":"166e28321a352483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":320,"wires":[]}]

```

  

> [@michaelbusry](#):
>
> I'm new here and rusty on my Java sorry

For the record. Java is a completely different language than what Node-RED runs on. Node-RED / NodeJS runs on JavaScript 🙂

---

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [13 October 2023 11:28 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/4 "2023-10-13T11:28:54Z")

</div>

I'm using 3.1 and running on Mac OS Sonoma. As far as code I'm using what I found in the How to Calculate CRC-16 (XModem)? thread:

```auto
1- Enter your home Node-RED directory ( ~/.node-red) and install there the NPM module

npm i crc16-xmodem
2- Edit the functionGlobalContext in setting.js, adding line below. Restart Node-RED

    functionGlobalContext: {
	crc16xmodem:require('crc16-xmodem'),
3- Require the module in a function node:

let crc = global.get("crc16xmodem");
// Calculate checksum with 4 digit hex output
const originalStr = msg.payload;
msg.payload = crc.calculateChecksum(originalStr);
return msg;

```

It's loading this string that's been parsed from a JSON file:

```auto
"iVBORw0KGgoAAAANSUhEUgAAAyAAAAEsCAAAAAAeRojmAAAAAXNCSVQI5gpbmQAABjdJREFUeJzt3N1y4jYAgFG70/d/ZfWiIdiy/EHSn92ZnHOxCdhIZqsPDN7pPjbgzh+/+gDgdyYQCAKBIBA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1v9h92np5K7g+8Liy52neON745TLgdXz8f7GOVicY++M06PN0aTx/Hk7IxnXrYdRxGfAwxHmk0zznB0/TzCPPz2QsHnJ+jtfjvh7+D+cd5Gn1uvl53zj8+fnr+NxhHLa+XGCPQcdipPM881CjNi6Oeprp+hxPA15uIpCn1Xr7vO/ytnIy5sfFGjt+dL5ZsHf3XKa5n2esd5me4zTgdXwE8mms19t/sWj+4y+L7j9C3DxHbvma9+Cydi4rbfH5+vIpbn98nXuzFsd+Hnk10vP95X6o2437eubd+dM3eAd5Wq6dOkOZ7dNv3/4yaL5asvxu9s15zhuXR7/nzZ/NO0jYFzdevAIfvyo6vCfM51RR2jTPZajj1uXG1RvIyzU/8uYPJpB7q1VcK+e4CkcsyvHGS/T567JVqa/n+Zjq9bXEvPnDOcX6mrh0fezp45vbWmz9Iv2cZx7qlO1inn2u65XrdRpvIE/eQW7NX4l+3Hd3aTUX1vR1ar7o9zxvrN/zydj++PO949bHmXeQ8v51h+sL+4t11otwvoxxutB3u/Gxw3moMer6hj6aQO68+HdO0yX282/fm+j6be3Sy4v0+77vf/987wB8QA8CCW+eMW3bvJrf/5b35Wo8D7X8PP3GPC9qf73fjyWQG3k1ern3WGz/J6/G4zrU4jzu7Xlur4A4wSo+pK9dVs75Asf294fo5TdL88bXcz137nmu6/cyz/HDyumDy3rmTR8v+OfuZ491Na266RLH9MHh/srEtPVw9e+6eueRpuMY1xIW//BlfiKrDzifs8eTFMoHp1hfNeLWfOdXVtn6kmQO9YV5rPdvcor1ls+X13G+42bdjdq4HnocbxzX/r8xjzy+zSkWBKdYEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEP4C+s1CgOrS/icAAAAASUVORK5CYII="

```

---

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [13 October 2023 11:30 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/5 "2023-10-13T11:30:20Z")

</div>

No. I'll. try it now. Thanks.

---

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [13 October 2023 11:52 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/6 "2023-10-13T11:52:28Z")

</div>

@Steve-Mcl comparing the result to [crccalc.com](http://crccalc.com) it's not correct.

```auto
"DocumentImage": "iVBORw0KGgoAAAANSUhEUgAAAyAAAAEsCAAAAAAeRojmAAAAAXNCSVQI5gpbmQAABjdJREFUeJzt3N1y4jYAgFG70/d/ZfWiIdiy/EHSn92ZnHOxCdhIZqsPDN7pPjbgzh+/+gDgdyYQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIf/6SWfdt27ZtbNu+beNxzzjvMA57bueb43z/ftjh87H7zW7XR11/OR7kdjnC6eCXw07PbvmUt5tnsa+eSzyLu8Obptju/4qmSadBvvT3tnz61/9qMcJykGmH/5F3EAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBA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j6aQO68+HdO0yX282/fm+j6be3Sy4v0+77vf/987wB8QA8CCW+eMW3bvJrf/5b35Wo8D7X8PP3GPC9qf73fjyWQG3k1ern3WGz/J6/G4zrU4jzu7Xlur4A4wSo+pK9dVs75Asf294fo5TdL88bXcz137nmu6/cyz/HDyumDy3rmTR8v+OfuZ491Na266RLH9MHh/srEtPVw9e+6eueRpuMY1xIW//BlfiKrDzifs8eTFMoHp1hfNeLWfOdXVtn6kmQO9YV5rPdvcor1ls+X13G+42bdjdq4HnocbxzX/r8xjzy+zSkWBKdYEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEAQCQSAQBAJBIBAEAkEgEP4C+s1CgOrS/icAAAAASUVORK5CYII="

```

---

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [13 October 2023 12:08 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/7 "2023-10-13T12:08:57Z")

</div>

@Steve-Mcl I figured it out and got the correct value, using the JSON string. Now to get it down to the last four. As @zenofmud corrected me my Java is rusty by JavaScript is basically a newbie 😃

My result is msg.payload : number 0xed70. I need the ed70. quick ideas?

---

<div class="post-metadata">

**Author:** ![michaelbusry](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@michaelbusry](https://discourse.nodered.org/u/michaelbusry)\
**Post date:** [13 October 2023 14:38 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/8 "2023-10-13T14:38:58Z")

</div>

I figured it out. Thank you for all your help. @Steve-Mcl and @zenofmud

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 December 2023 14:39 UTC](https://discourse.nodered.org/t/creating-crc16-xmodem-hex-value/81981/9 "2023-12-12T14:39:17Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
