# Whats the best way to work out this checksum?

**URL:** <https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414>\
**Category:** General\
**Created:** [30 August 2021 22:47 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414 "2021-08-30T22:47:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [30 August 2021 22:47 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/1 "2021-08-30T22:47:43Z")

</div>

Guys,

I am working through the control of my new inverters and batteries and have worked out the next stage of sending data to them in the correct format.

Basically it is a hex string such as

aa55c07f02390b070d04000006000005ff7f0425

The last two bytes are the checksum which are calculated by a simple addition of all the preceeding bytes.

In this instance the one value that will change is the 05 towards the end of the string and this could be any number between decimal 1 and 100

I could be calculating this every 10 seconds so should not be a big load on the PI

For this particular instance the string will remain the same length - but for some of the other functions it will vary.so i would like to write a generic checksum routine that i call at the end of each calculation

What would be the most effective way ? Read it into an array and cycle through all the elements in the array and add them from there ?

Craig

---

<div class="post-metadata">

**Author:** ![RaptorUK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raptoruk/32/46988_2.png) [@RaptorUK](https://discourse.nodered.org/u/RaptorUK)\
**Post date:** [31 August 2021 09:03 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/2 "2021-08-31T09:03:19Z")

</div>

> [@craigcurtin](#):
>
> Basically it is a hex string such as
> 
> aa55c07f02390b070d04000006000005ff7f0425

There are many ways . . . I'm far from being a Node Red or JavaScript expert but If I were doing this I'd use a function node and pass in the string as a message . . .

I presume you mean the first two bytes as you talk about the preceding bytes ?

W3 School - Javascript is your friend [JavaScript Tutorial](https://www.w3schools.com/js/default.asp)

```auto
[{"id":"fe9880b4.a24d9","type":"function","z":"b7065b03.85e128","name":"","func":"var stringarray, index, runningtotal = 0, checksum = \"\";\n\nstringarray = msg.payload.split(\"\"); // creates an array of the characters in the string\n \nfor (index = 0; index < stringarray.length; index++)\n {\n runningtotal += parseInt(stringarray[index], 16)\n }\n\nchecksum = runningtotal.toString(16);\nmsg.payload = msg.payload + checksum;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":1380,"wires":[["55ed4db9.070024"]]},{"id":"dc4022bd.78d48","type":"inject","z":"b7065b03.85e128","name":"value","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"aa55c07f02390b070d04000006000005ff7f0425","payloadType":"str","x":290,"y":1380,"wires":[["fe9880b4.a24d9"]]},{"id":"55ed4db9.070024","type":"debug","z":"b7065b03.85e128","name":"inc checksum","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":654.2857055664062,"y":1381.4285888671875,"wires":[]}]

```

---

<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:** [31 August 2021 09:25 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/3 "2021-08-31T09:25:15Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e148a2acda377d86844978918af18a775cc248c9.png)

```javascript
let chk = 0x0;
let buf = Buffer.from(msg.payload, "hex"); 
 
for (let index = 0; index < buf.length; index++) {
    chk += (buf[index] & 0xFFFF)
}

msg.originalPayload = msg.payload;
msg.checksum = chk;
msg.checksumHex = chk.toString(16).padStart(4, "0");
msg.payload = msg.payload + msg.checksumHex;
return msg;

```

This should get you close.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [31 August 2021 09:27 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/4 "2021-08-31T09:27:50Z")

</div>

Hey mate - thanks for taking the time.

Not sure it is working though

If i drop the last 4 characters (2 hec characters off the end) then the rest should add up to that - when i do this - it does not spit out the 0425 i would expect.

To work these out i have been just adding every pair into calc in hex mode and they have been working

so

aa + 55 + c0 etc etc

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [31 August 2021 09:32 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/5 "2021-08-31T09:32:34Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> let chk = 0x0;
> let buf = Buffer.from(msg.payload, "hex"); 
>  
> 
> ```

Yep that looks like thanks Steve - much appreciated

Craig

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 August 2021 19:48 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/6 "2021-08-31T19:48:22Z")

</div>

Didn't you start off with this as a buffer in another thread, or am I confused? If so then there is no point converting it to a string and then back to a buffer.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [31 August 2021 20:15 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/7 "2021-08-31T20:15:10Z")

</div>

Colin,

You are right i did start off with this as a buffer - but i then need to manipulate it to change some values (this represents the running state of my inverter in terms of charge/discharge, percentage of battery remaining etc)

I need to change some of the value on the fly (for instance go from charge a 10% to discharge at 30%) and when i change those i need to recalculate the checksum to send back to the inverter

Craig

---

<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:** [14 September 2021 20:15 UTC](https://discourse.nodered.org/t/whats-the-best-way-to-work-out-this-checksum/50414/8 "2021-09-14T20:15:53Z")

</div>

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