Thursday, August 27, 2009

Checksum error: differs by 1 or 2

When you calculate checksum, if it varies by just with last two digits (value varies by 0x01 or 0x02 or 0x03), then the problem is that the carry has not been added.
For example, the correct checksum is 0x5e94. But, your calculation is 0x5e96. To solve this problem, check whether the carry has been added.

csum += *(unsigned short)data;
:
csum = (csum & 0xffff) + (csum >> 16); /* Please add this carry too */
csum = csum + (csum >> 16); /* If the carry is again generated, it has to be added */

No comments: