(1)四位16进制数分别相加

(2)相加的结果去掉进位,保留四位16进制数

(3)保留的四位16进制数+进位

(4)得到的结果转换成二进制

(5)取反

(6)再转换成16进制数

具体编码如下:

int checksum(unsigned long* buffer, int size)

{

    unsigned long cksum = 0;

    while(size>1)

    {

        cksum += *buffer++;

        size -= sizeof(unsigned long);

    }

    if(size)

    {

        cksum += *(char*)buffer;

    }

    cksum = (cksum>>16) + (cksum&0xffff); 

    cksum += (cksum>>16); 

    return (unsigned long)(~cksum);

}