unsignedlonglong end = 512;
unsignedlonglong checksum = 0;
unsignedshortint twoByteLoingCheckSum = 0;
// Add all of the bytes in the array together
for(unsignedlonglong i = 0; i < end; i++){
checksum = checksum + (unsignedint)((unsignedchar)buffer[i]);
}
// invert the bits for the first 2 bytes
for(int t = 32768; t > 0; t = t / 2){
if(checksum & t) checksum = checksum - t;
else checksum = checksum + t;
}
// Add 1
checksum = checksum + 1;
// Print only the first two (LSBs) bytes of the checksum
unsignedshortint twoByteLongCheckSum = (unsignedshortint)checksum;
printf("\n\t2's Compliment Checksum: %04X", twoByteLongCheckSum);
Can anyone see something wrong with it? because it is not calculating the expected value for me!
Furthermore, even if you print as signed, the beauty of 2's compliment is that it works the same with both signed and unsigned numbers, so you don't have to worry about details.
If you want a 16 bit signed number, just make one:
1 2 3 4 5 6 7 8
short checksum = 0; // 16-bit signed
for(i = 0; i < end; ++i)
checksum += buffer[i]; // maybe cast buffer[i] to unsigned/signed char depending on how
// you're supposed to interpret that data
// print as a signed, 16-bit, 2's compliment checksum:
printf("%d", checksum);
Thanks that was very helpful. I think I was trying over complicate things :)
Also for anyone reaching this page via google, it might be useful to know that if your checksum is not what you expect it to be, try calculating using blocks of 2 bytes from your data set, rather than 1. Took me a while to figure that one out ^_^