分类: C/C++
2008-06-06 10:39:30
unsigned short checksum(unsigned short *buffer,int size) //校验和的求法
{
unsigned long cksum=0;
while(size>0) //各位求和
{
cksum+=*buffer++;
size-=sizeof(unsigned short);
}
if(size)
cksum+=*(unsigned char *)buffer;
cksum=(cksum>>16)+(cksum & 0xffff); //移位,位与运算
cksum+=(cksum>>16);
return (unsigned short)(~cksum); //再取反
}