21,886
社区成员
发帖
与我相关
我的任务
分享
function getchksum($data)
{
//return the chksum of the data
$chksum = 0;
$temp = 0;
for($e=0; $e<count($data)/4; $e+=4)
{
$temp = unpack("V1a", substr($data, $e, 4));
$temp = sprintf("%u",$temp['a']);
//print("$temp + $chksum = " . ($temp+$chksum)."<br />");
$chksum = sprintf("%u", ($chksum+$temp) & 0xFFFFFFFF);
}
$temp = $chksum>>16;
$temp = sprintf("%u", ($chksum+$temp)&0xFFFF);
return $temp;
}
$temp *= $chksum >> 16;
function getchksum($data){
//returnthechksumofthedata
$chksum=0;
$temp=0;
for($e=0;$e<strlen($data)/4;$e+=4){
$temp=unpack("V1a", substr($data, $e, 4));
$temp=sprintf("%u",$temp['a']);
//print("$temp+$chksum = " . ($temp+$chksum)."<br/>");
$chksum=sprintf("%u", ($chksum+$temp)& 0xFFFFFFFF);
}
$temp = $chksum>>16;
$temp = sprintf("%u", ($chksum+$temp) & 0xFFFF);
return$temp;
}
echo getchksum("sending data...");
$c = 0xFA;
if( $c < 200 )
{
echo sprintf("%04x",$c);
}
unsigned short in_cksum(unsigned short *addr, int len)
{
register int nleft = len;
register unsigned short *w = addr;
register int sum = 0;
unsigned short answer =0;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}