# How do I sum the values of a buffer

**URL:** <https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708>\
**Category:** General\
**Created:** [18 January 2025 05:07 UTC](https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708 "2025-01-18T05:07:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![redhead](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@redhead](https://discourse.nodered.org/u/redhead)\
**Post date:** [18 January 2025 05:07 UTC](https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708/1 "2025-01-18T05:07:21Z")

</div>

I need to calculate the sum of the HEX values of a buffer for my serialport string checksum value.  
for example, here is the buffer:  
msg.payload = Buffer.from([0xca,0x07,0x06,0x01,0x17,0x01,0x00,0x00,0x00,0x00,0x26,0x35]);  
To get the checksum I need to sum index 1 through 9.  
The sum of 0x07,0x06,0x01,0x17,0x01,0x00,0x00,0x00,0x00 = 0x26 = checksum  
Index 10 is the checksum in the buffer

I would also like to know what the best way would be to create the buffer once I have calculated the checksum.  
My guess is I would use "concat". If you have any examples, you can share that would be great.

A variable value is being sent in the serialport message, so the checksum will change everytime the variable value changes.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [18 January 2025 06:34 UTC](https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708/2 "2025-01-18T06:34:07Z")

</div>

I am not super familiar with buffer array's but apperently you can treat them like normal arrays.  
According to AI:

```auto
// input would be msg.payload
const input = [0xca, 0x07, 0x06, 0x01, 0x17, 0x01, 0x00, 0x00, 0x00, 0x00, 0x26, 0x35] 
const buffer = Buffer.from(input)

// Calculate the checksum by summing up values from index 1 to 9
let checksum = 0;
for (let i = 1; i <= 9; i++) {
    checksum += buffer[i];
}

// Convert the calculated checksum to a hexadecimal string and ensure it matches the expected value at index 10
const expectedChecksum = buffer[10].toString(16).toUpperCase();
const actualChecksum = (checksum & 0xFF).toString(16).toUpperCase(); // Ensure we only consider the last byte of the sum

node.warn(`Expected Checksum: ${expectedChecksum}`);
node.warn(`Actual Checksum: ${actualChecksum}`);

// Verify if they match
if (expectedChecksum === actualChecksum) {
    node.warn("The checksum is correct.");
} else {
    node.warn("There is an error in the checksum calculation.");
}

```

---

<div class="post-metadata">

**Author:** ![redhead](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@redhead](https://discourse.nodered.org/u/redhead)\
**Post date:** [18 January 2025 07:57 UTC](https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708/3 "2025-01-18T07:57:40Z")

</div>

> [@bakman2](#):
>
> ```auto
> // input would be msg.payload
> const input = [0xca, 0x07, 0x06, 0x01, 0x17, 0x01, 0x00, 0x00, 0x00, 0x00, 0x26, 0x35] 
> const buffer = Buffer.from(input)
> 
> // Calculate the checksum by summing up values from index 1 to 9
> let checksum = 0;
> for (let i = 1; i <= 9; i++) {
> checksum += buffer[i];
> }
> 
> // Convert the calculated checksum to a hexadecimal string and ensure it matches the expected value at index 10
> const expectedChecksum = buffer[10].toString(16).toUpperCase();
> const actualChecksum = (checksum & 0xFF).toString(16).toUpperCase(); // Ensure we only consider the last byte of the sum
> 
> ```

Thanks - this was very helpful.

---

<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:** [1 February 2025 07:58 UTC](https://discourse.nodered.org/t/how-do-i-sum-the-values-of-a-buffer/94708/4 "2025-02-01T07:58:19Z")

</div>

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