From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

# LabVIEW

cancel
Showing results for
Did you mean:

## checksum

Hi,

I have to calculate the checksum using the function below. I am new to Labview and my c skills is a little rysty. Please help.

Thankyou.

24 bit  = 00001 + 0000000000000000000

Var1 (5bit msb)

Var2 (19bit)

Encoded little endian 32 bit from 0x1FFFC to 0x1FFFF

uint32_t checksum8(const void *buf, uint32_t length)

{

uint32_t n, checksum=0;

uint8_t *p = (uint8_t *)buf;

for (n=0; n <length; n++)

{

checksum += *p++;

if (checksum > 255) checksum -= 255;

}

return (~checksum&0xff);

Combined number = checksum * 16777216 + Var1 * 524288 + Var2

Message 1 of 11
(3,092 Views)

## Re: checksum

Hi labvcj,

here's the inner loop to give you a starter:

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 11
(3,063 Views)

## Re: checksum

I'd use a quotient and remainder

"Should be" isn't "Is" -Jay
Message 3 of 11
(3,045 Views)

## Re: checksum

Hi Jeff,

this is what the OP wants to implement:

`if (checksum > 255) checksum -= 255;`

I'd use a quotient and remainder

I thought so too at first, but what would be the result when checksum is equal to 255?

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 11
(3,028 Views)

## Re: checksum

Hi Jeff,

some more explanation (too late to edit previous post):

- "(checksum > 255)" refers to a Q&R with 256 as divider…

- "checksum -= 255" refers to a Q&R with 255 as divider…

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 5 of 11
(3,019 Views)

## Re: checksum

Seems like a strange choice of algorithm for the checksum.

In a normal world, for an 8 bit checksum, you would get the byte array and use sum array elements on it (the result would be limited to 8 bits and would all be fine).

In this case, each time you overflow the 8 bits you effectively need to discard the overflowed bit add 1 to the checksum.

It doesn't add any robustness to the checksum algo, and feels to me like a mistake.

How sure are we that the c++ code actually works?

Message 6 of 11
(3,006 Views)

## Re: checksum

@GerdW wrote:

Hi Jeff,

some more explanation (too late to edit previous post):

- "(checksum > 255)" refers to a Q&R with 256 as divider…

- "checksum -= 255" refers to a Q&R with 255 as divider…

Sorry,  I should have been specific.  Tha line is equivalent to

checksum = 256 mod (checksum);

Or, just use a U8 😄 and no math required.

You know where to post your example.

Better you than Christian.  😄

"Should be" isn't "Is" -Jay
Message 7 of 11
(2,991 Views)

## Re: checksum

Hi Jeff,

How is

`checksum = 256 mod (checksum);`

equivalent to

`if (checksum > 255) checksum -= 255;`

???

Example:

checksum := 270 (before math routine).

Your result: checksum = (256 mod 270) = 256

OP wanted: checksum = 270-255 = 15

Best regards,
GerdW

using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 11
(2,985 Views)

## Re: checksum

256 mod (270) is 14. Modulus is the remainder of the dividend ,270 / divisor, 256.

My C++ may be rusty but,  is that a bad thing for a  LabVIEW guy?

"Should be" isn't "Is" -Jay
Message 9 of 11
(2,982 Views)

## Re: checksum

@GerdW wrote:

Hi Jeff,

How is

`checksum = 256 mod (checksum);`

equivalent to

`if (checksum > 255) checksum -= 255;`

???

Example:

checksum := 270 (before math routine).

Your result: checksum = (256 mod 270) = 256

OP wanted: checksum = 270-255 = 15

Jeff's nomenclature is backwards.  It really should be:

`checksum mod 255`

That would match what the OP stated.  If you just add bytes and ignore the overflow (the way much checksums work), it would be a mod 256.

There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 10 of 11
(2,978 Views)