Internet校验和计算示例

Packet

01 00 F2 03 F4 F5 F6 F7 00 00

(00 00 is the checksum field)

Form the 16-bit words

0100 F203 F4F5 F6F7

Calculate 2’s complement sum

0100 + F203 + F4F5 + F6F7 = 0002 DEEF (store the sum in a 32-bit word)

Add the carries (0002) to get the 16-bit 1’s complement sum

DEEF + 002 = DEF1

Calculate 1’s complement of the 1’s complement sum

~DEF1 = 210E

We send the packet including the checksum 21 0E

01 00 F2 03 F4 F5 F6 F7 21 0E

At the receiving

0100 + F203 + F4F5 + F6F7 + 210E = 0002 FFFD
FFFD + 0002 = FFFF

which checks OK.

原文链接:https://blog.csdn.net/qq_34369618/java/article/details/60603867

翻译一下:

有一个Packet

01 00 F2 03 F4 F5 F6 F7 00 00

(最后四位 00 00 是校验和)

把上面十六位的数据两个两个组合

0100 F203 F4F5 F6F7

将这四个32位的16进制数相加

0100 + F203 + F4F5 + F6F7 = 0002 DEEF

最高位进位的“1”返回到最低位继续加

DEEF + 0002 = DEF1

再对其结果按位取反

~DEF1 = 210E

21 0E就是这个package的校验

01 00 F2 03 F4 F5 F6 F7 21 0E

接收端也是一样的,两个十六位的数组成一个三十二位的数,再将其全部相加

0100 + F203 + F4F5 + F6F7 + 210E = 0002 FFFD
FFFD + 0002 = FFFF

得到的结果为FFFF就没有问题