首部校驗和

(1)四位16進制數分別相加ide

(2)相加的結果去掉進位,保留四位16進制數編碼

(3)保留的四位16進制數+進位it

(4)獲得的結果轉換成二進制class

(5)取反二進制

(6)再轉換成16進制數di

具體編碼以下:view

int checksum(unsigned long* buffer, int size)vi

{while

    unsigned long cksum = 0;co

    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);

}

相關文章
相關標籤/搜索