From 11:00 PM CDT Friday, Nov 8 - 2:30 PM CDT Saturday, Nov 9, 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 
Search instead 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

 

0 Kudos
Message 1 of 11
(3,562 Views)

Hi labvcj,

 

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

check.png

Best regards,
GerdW


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

I'd use a quotient and remainder


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

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
0 Kudos
Message 4 of 11
(3,498 Views)

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,489 Views)

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?

 

0xDEAD

Message 6 of 11
(3,476 Views)

@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
0 Kudos
Message 7 of 11
(3,461 Views)

Hi Jeff,

 

I don't get your math…

 

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
0 Kudos
Message 8 of 11
(3,455 Views)

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
0 Kudos
Message 9 of 11
(3,452 Views)

@GerdW wrote:

Hi Jeff,

 

I don't get your math…

 

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.

 

 


GCentral
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
(3,448 Views)