App-7
IM 04L51B01-17EN
Appendix
App
Appendix 5 Check Sum Calculation Method
The check sum of binary data is calculated using an algorithm like the one shown below.
int CalcSum(unsigned char *buf, int len)
{
int odd;
unsigned long sum;
unsigned char *p;
sum = 0;
odd = len & 1;
len >>= 1;
for (p = buf ; len ; len --, p += 2)
{
sum += (*p << 8) | *(p + 1);
}
if (odd) sum += (*p << 8);
sum = (sum & 0xffff) + ((sum >> 16) & 0xffff);
if (sum > 0xffff) sum = sum - 0xffff;
return ((~sum) & 0xffff);
}