Skip to content

Instantly share code, notes, and snippets.

@fanfeilong
Last active August 29, 2015 14:05
Show Gist options
  • Save fanfeilong/844ad0c2e2654cfd4c7e to your computer and use it in GitHub Desktop.
Save fanfeilong/844ad0c2e2654cfd4c7e to your computer and use it in GitHub Desktop.
编码

字符编码


  • ASCII
  • EASCII
  • ISO 8859
    • ISO 8859-n(n=1,2,3,...,11,13,...,16)
    • Latin-1==ISO8859-1
  • GB2312
  • GBK > GB2312
  • BIG5 (small conflict with GB2312)
  • GB18030 > GB2312
  • Unicode (NOT Compatible with GBXXX)
    • UCS2
    • UTF-16 (Extend UCS2)
    • UCS4
    • UTF-32 (Currently a subset of UCS4, But ability to encode more unicode characters)
    • UTF-8
      • BOM
      • No BOM

反码


本来用1反码表示负数就够了,但是有缺陷

  • 0必须有-0表示
    • 这是因为在1-反码的情况下,为了在1-反码的二进制加减法规则下让N+(-N)=N-N,才需要定义-0
  • 二进制加减法会有溢出,需要对溢出的字节做环回处理
  • 表示范围只能是[-2^(n-1)-1,2^(n-1)]

使用2-反码(等于1-反码+1)表示负数则

  • 0只需要唯一表示
  • 二进制加减法不需要对溢出的字节做处理
  • 表示范围是[-2^(n-1),2^(n-1)]

比如IPV4的Checksum:

The checksum field is the 16-bit one's complement of the one's complement sum of all 16-bit words in the header.

实际计算算法:

  1. 把头部的每16个位直接按32bit数加起来得到一个32位数
  2. 把得到的32位数拆成高16位和低16位,把高16位加到低16位
  3. 取反

The checksum field is the 16-bit one's complement of the one's complement sum of all 16-bit words in the header.

这句话的sum of all 16-bit words代表的是计算步骤的1-2步 这句话的the 16-bit one's complement of the one's complement..代表的是计算步骤的第3步

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment