#include stdio.h
#include string.h
int DataLen=5;
const char* pData="ABCDE";
unsigned short CalculateChecksum (const unsigned char* pData, int DataLen);
main()
{
unsigned long CS = 0;
unsigned short* psData = (unsigned short*)pData;
for (int Index = 0; Index < (DataLen / 2); Index++, psData++)
CS += *psData;
if (DataLen % 2 != 0)
CS += pData[DataLen - 1];
CS = (CS >> 16) + (CS & 0xFFFF);
CS += (CS >> 16);
printf("%4x", CS);
return (unsigned short) ~CS;
}
-----------------------------
ABCDE
step1.
AB->BA
CD->DC
E->0E
Step2.
Sum
BA+DC+0E
---------------------------------------------
step3.
When you get to lager sums carry must be added back to the original number.
ex. x12345
=x1234+5=x1239