NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble calculating a checksum.

I'm trying to generate a 16 bit checksum from a .hex file I have, I've tried breaking the hexfile into a 1D array of 65476 elements and summing the individual elements but I keep getting the wrong answer.  Does anyone have an example of how to create a checksum from a file?  I've included what I have that isn't working.  I'm ignoreing the first 40K some odd elements because I know they are 0.
 
Thanks.
 
 
 
The equivalent C++ code is
 

for( UINT i = 0; i < (dwFileLength >> 1); i++ )

{

if ((pwImage[ i ] & 0xFFFF) != 0)

{

iChecksumPc += pwImage[ i ] & 0xFFFF;

}

}

 

0 Kudos
Message 1 of 2
(3,347 Views)
Hi
 
The reason that you are not getting the right checksum is probably because you are reading two hex characters at a time (in the for loop for the read). The two characters would only make up an 8 bit value and a not a 16 bit value. Therefore, I would recommend you to read 4 characters at time to perform a 16 bit checksum.
 
I hope this helpsl.
 
Mehak Dinesh
Applications Engineer
National Instruments
 
 
Mehak D.
0 Kudos
Message 2 of 2
(3,309 Views)