有关checksum的问题,不知道原因,还烦请高手指点一下,多谢了.
[复制链接]
小弟不才,学习checksum算法的时候,对如下数据始终无法校验成功,麻烦各位大哥帮忙指点下。
对两段报文,用相同的机制,一个校验成功,一个失败,但这个两个包都是对的,我不知道原因,麻烦哥指点一下。多谢了。
static u8 packet1[] =
{
/*0x38 ,0x8b为checksum字段*/
0x02, 0xb4, 0x38, 0x8b, 0x00, 0x01, 0x00, 0x0a, 0x53, 0x77,
0x69, 0x74, 0x63, 0x68, 0x00, 0x05, 0x00, 0xbc, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x4f,
0x53, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x2c, 0x20, 0x43, 0x32, 0x39, 0x36,
0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39, 0x36,
0x30, 0x2d, 0x4c, 0x41, 0x4e, 0x42, 0x41, 0x53, 0x45, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 0x56, 0x65,
0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x32, 0x28, 0x32, 0x35, 0x29, 0x53, 0x45,
0x45, 0x32, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f, 0x46, 0x54,
0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72,
0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30,
0x30, 0x36, 0x20, 0x62, 0x79, 0x20, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74,
0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
0x65, 0x64, 0x20, 0x46, 0x72, 0x69, 0x20, 0x32, 0x38, 0x2d, 0x4a, 0x75, 0x6c, 0x2d, 0x30, 0x36,
0x20, 0x30, 0x34, 0x3a, 0x33, 0x33, 0x20, 0x62, 0x79, 0x20, 0x79, 0x65, 0x6e, 0x61, 0x6e, 0x68,
0x00, 0x06, 0x00, 0x19, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39,
0x36, 0x30, 0x2d, 0x32, 0x34, 0x54, 0x43, 0x2d, 0x4c, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x14, 0x46, 0x61,
0x73, 0x74, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x32, 0x31, 0x00, 0x04,
0x00, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x08, 0x00, 0x24, 0x00, 0x00, 0x0c, 0x01, 0x12, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x20, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0xa1, 0xed, 0x0e, 0x00, 0xff, 0x00, 0x00, 0x00, 0x09, 0x00, 0x11, 0x6d, 0x61,
0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x00, 0x0a, 0x00, 0x06, 0x00,
0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00,
0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00
};
static u8 packet[] =
{
/*0xba,0x50为checksum字段*/
0x02, 0xb4, 0xba, 0x50, 0x00, 0x01, 0x00, 0x0a, 0x53, 0x77,
0x69, 0x74, 0x63, 0x68, 0x00, 0x05, 0x00, 0xbc, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x4f,
0x53, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x2c, 0x20, 0x43, 0x32, 0x39, 0x36,
0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39, 0x36,
0x30, 0x2d, 0x4c, 0x41, 0x4e, 0x42, 0x41, 0x53, 0x45, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 0x56, 0x65,
0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x32, 0x28, 0x32, 0x35, 0x29, 0x53, 0x45,
0x45, 0x32, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f, 0x46, 0x54,
0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72,
0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30,
0x30, 0x36, 0x20, 0x62, 0x79, 0x20, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74,
0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
0x65, 0x64, 0x20, 0x46, 0x72, 0x69, 0x20, 0x32, 0x38, 0x2d, 0x4a, 0x75, 0x6c, 0x2d, 0x30, 0x36,
0x20, 0x30, 0x34, 0x3a, 0x33, 0x33, 0x20, 0x62, 0x79, 0x20, 0x79, 0x65, 0x6e, 0x61, 0x6e, 0x68,
0x00, 0x06, 0x00, 0x19, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39,
0x36, 0x30, 0x2d, 0x32, 0x34, 0x54, 0x43, 0x2d, 0x4c, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x14, 0xde, 0x00, 0x03, 0x00, 0x14, 0x46, 0x61,
0x73, 0x74, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x32, 0x31, 0x00, 0x04,
0x00, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x08, 0x00, 0x24, 0x00, 0x00, 0x0c, 0x01, 0x12, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x21, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1a, 0xa1, 0xed, 0x0e, 0x00, 0xff, 0x00, 0x00, 0x00, 0x09, 0x00, 0x11, 0x6d, 0x61,
0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x00, 0x0a, 0x00, 0x06, 0x00,
0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00,
0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x14,
0xde
}
unsigned short check_sum( unsigned char* data, int length )
{
register u32 sum = 0;
register const u16 *d = ( const u16 * )data;
while ( length > 1 ) {
sum += *( u16* )d++;
length -= 2;
}
if ( length )
sum += *( const u8* )d;
sum = ( sum >> 16 ) + ( sum & 0xffff );
sum += ( sum >> 16 );
return ( u16 )~sum;
}
void main()
{
unsigned int checksum;
checksum = check_sum( packet,363);
printf("%d\r\n",checksum );
checksum = check_sum( packet1,363);
printf("%d\r\n",checksum );
}
这样计算后由packet1计算的checksum为0而packet的checksum却不为0,但两个数据绝对没有错误,麻烦大哥们指点一下。
|