# Calculate a checksum

**URL:** <https://discourse.nodered.org/t/calculate-a-checksum/20142>\
**Category:** General\
**Created:** [9 January 2020 23:03 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142 "2020-01-09T23:03:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [9 January 2020 23:03 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/1 "2020-01-09T23:03:21Z")

</div>

Hi

I need to calculate a checksum i a buffer payload

the way to calculate the checksum is as follow

1. In Windows, Start / Programs / Accessories / Calculator

2. In Calculator, under View, Select Scientific

3. In Calculator, select Hex

4. Add the first 7 Hex bytes, then press = (ie: 1C + 01 + 20 + 00 + 00 + 00 + FF total = 13C)

5. Press the [Not] button (in our example, the total = FFFFFEC3)

6. Add 1 to the total (in our example, the total = FFFFFEC4)

7. The checksum is the two least significant characters (in our example, checksum = C4)

I have written some code in a function, but i am stuck on the 5) NoT and 7)  
How do I convert the Not ~ to a buffer i can insert in my execisting buffer, how do i only take the last tow figures on the final result and but it on D7

```auto
D0 = [0x1C]
D1 = msg.payload.A
D2 = msg.payload.B
D3 = msg.payload.C
D4 = [0x00]
D5 = [0x00]
D6 = [0xff]
D7 = [0] //Checksum

//add = D0 + D1 + D2 + D3 + D4 + D5 + D6 
add = D1 + D2 + D3
not = ~ add
plus_one = not + 1

// Q1: Convert plus_one to a buffer.
//Q2: Take out the last two characters an put it in to D7

msg.payload = new Buffer([D0,D1,D2,D3,D4,D5,D6,D7]);

return msg;

```

```auto
[{"id":"f131a05.069bde","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"16421c01.36c1d4","type":"function","z":"f131a05.069bde","name":"Math add","func":"D0 = [0x1C]\nD1 = msg.payload.A\nD2 = msg.payload.B\nD3 = msg.payload.C\nD4 = [0x00]\nD5 = [0x00]\nD6 = [0xff]\nD7 = [0] //Checksum\n\n//add = D0 + D1 + D2 + D3 + D4 + D5 + D6 \nadd = D1 + D2 + D3\nnot = ~ add\nplus_one = not + 1\n\n\n\nmsg.payload = new Buffer([D0,D1,D2,D3,D4,D5,D6,D7]);\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":200,"wires":[["348f24e7.aed91c"]]},{"id":"348f24e7.aed91c","type":"debug","z":"f131a05.069bde","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":280,"wires":[]},{"id":"f5c82250.e845f","type":"inject","z":"f131a05.069bde","name":"","topic":"","payload":"{\"A\":1,\"B\":32,\"C\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":200,"wires":[["16421c01.36c1d4","348f24e7.aed91c"]]}]

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [10 January 2020 17:22 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/2 "2020-01-10T17:22:53Z")

</div>

Isn’t there any guinness, that can help a little

---

<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:** [10 January 2020 17:45 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/3 "2020-01-10T17:45:18Z")

</div>

Did you try a google search 'javascript checksum'?

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [10 January 2020 18:18 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/4 "2020-01-10T18:18:38Z")

</div>

> [@zenofmud](#):
>
> javascript checksum'

Yes unfortunately with no luck, i’m still new to Javascript, that’s why I did write this post and hopefully someone could give me some guidance

---

<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:** [10 January 2020 18:42 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/5 "2020-01-10T18:42:03Z")

</div>

What kind of check sum are you trying to create?

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [10 January 2020 18:58 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/6 "2020-01-10T18:58:45Z")

</div>

It’s I checksum for Philips Dynalite dynet1 commands

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 January 2020 20:24 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/7 "2020-01-10T20:24:45Z")

</div>

Hi @zamzon

based on your initial description of the checksum algorithm, I think the following will do it:

```auto
 var D = [
    0x1C,
    msg.payload.A,
    msg.payload.B,
    msg.payload.C,
    0x00,
    0x00,
    0xff,
    0x00
]

var sum = D[0]+D[1]+D[2]+D[3]+D[4]+D[5]+D[6];

D[7] = (-sum>>>0)&0xff;

msg.payload = Buffer.from(D);

return msg;

```

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [10 January 2020 22:02 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/8 "2020-01-10T22:02:13Z")

</div>

I dont understand your D[7] but it works perfect.  
THANKS 😄

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 January 2020 23:31 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/9 "2020-01-10T23:31:07Z")

</div>

D[7] is the last element of the array D - it's initially set to 0x00 to reserve a slot for it. - then once the value is calculated it is inserted into that place.

---

<div class="post-metadata">

**Author:** ![zamzon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zamzon/32/15521_2.png) [@zamzon](https://discourse.nodered.org/u/zamzon)\
**Post date:** [11 January 2020 09:36 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/10 "2020-01-11T09:36:32Z")

</div>

Yes that part I understand, it is the calculation I don’t understand

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 January 2020 09:51 UTC](https://discourse.nodered.org/t/calculate-a-checksum/20142/11 "2020-01-11T09:51:13Z")

</div>

> [@knolleary](#):
>
> `D[7] = (-sum>>>0)&0xff;`

Doing the `not/plus_one` steps is the same as negating the value - `-sum`.

To get the two least significant bits, we shift it across `>>>0` and then mask it `&0xff`
