Creating CRC16 xModem Hex Value

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.

@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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

Did you try this: CRC-32 can't get the correct check sum - any help appreciated - #5 by Steve-Mcl

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

Example:

[{"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":[]}]

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

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:

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:

"iVBORw0KGgoAAAANSUhEUgAAAyAAAAEsCAAAAAAeRojmAAAAAXNCSVQI5gpbmQAABjdJREFUeJzt3N1y4jYAgFG70/d/ZfWiIdiy/EHSn92ZnHOxCdhIZqsPDN7pPjbgzh+/+gDgdyYQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSAQCAKBIBAIAoEgEAgCgSA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YdRxGfAwxHmk0zznB0/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="

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

@Steve-Mcl comparing the result to crccalc.com it's not correct.

"DocumentImage": "iVBORw0KGgoAAAANSUhEUgAAAyAAAAEsCAAAAAAeRojmAAAAAXNCSVQI5gpbmQAAB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rS/icAAAAASUVORK5CYII="

@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 :smiley:

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

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

1 Like

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