Calculate a checksum

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

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

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

Did you try a google search '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

What kind of check sum are you trying to create?

It’s I checksum for Philips Dynalite dynet1 commands

Hi @zamzon

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

 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;

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

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.

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

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