• 多语言下CRC16算法实现(C,JavaScript,VB,Python)


    C

     1 #include <stdio.h>
     2 
     3 #define u16 unsigned int
     4 #define u8  unsigned char
     5 
     6 u16 CRC16_Check(u8 *Pushdata,u16 length)
     7 {
     8     u16 Reg_CRC=0xffff;
     9     u16 i,j;
    10 
    11     for( i = 0; i<length; i ++)
    12     {
    13         Reg_CRC^= *Pushdata++;
    14 
    15         for (j = 0; j<8; j++)
    16         {
    17             if (Reg_CRC & 0x0001){
    18                 Reg_CRC=Reg_CRC>>1^0xA001;
    19             }else{
    20                 Reg_CRC >>=1;
    21             }
    22         }
    23     }
    24     return (Reg_CRC);
    25 }
    26 
    27 
    28 int main(void)
    29 {
    30     printf("%d
    ",sizeof("crc16test")-1); // length 9
    31     printf("%x
    ",CRC16_Check((u8 *)"crc16test",sizeof("crc16test")-1)); // 2481
    32     return 0;
    33 }

    Python

     1 def crc16(x, invert=False):
     2     a = 0xFFFF
     3     b = 0xA001
     4     for byte in x:
     5         a ^= ord(byte)
     6         for i in range(8):
     7             last = a % 2
     8             a >>= 1
     9             if last == 1:
    10                 a ^= b
    11     s = hex(a).upper()
    12     return s[4:6]+s[2:4] if invert == True else s[2:4]+s[4:6]
    13 
    14 if __name__ == '__main__':
    15     print(crc16('crc16test')) # crc编码后  2481
    16     print(int(crc16('crc16test',16))) # 十进制  9345

    VB

     1 ' 查表法实现CRC16
     2 Private Function CRC16(data() As Byte) As String
     3   Dim CRC16Hi As Byte
     4   Dim CRC16Lo As Byte
     5   CRC16Hi = &HFF
     6   CRC16Lo = &HFF
     7   Dim i As Integer
     8   Dim iIndex As Long
     9   For i = 0 To UBound(data)
    10     iIndex = CRC16Lo Xor data(i)
    11     CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex)        '低位处理
    12     CRC16Hi = GetCRCHi(iIndex)                    '高位处理
    13   Next i
    14   Dim ReturnData(1) As Byte
    15   ReturnData(0) = CRC16Hi        'CRC高位
    16   ReturnData(1) = CRC16Lo        'CRC低位
    17   CRC16 = ReturnData
    18 End Function
    19 
    20 
    21 
    22 'CRC低位字节值表
    23 Function GetCRCLo(Ind As Long) As Byte
    24       GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, _
    25         &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, _
    26         &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, _
    27         &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, _
    28         &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, _
    29         &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _
    30         &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, _
    31         &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, _
    32         &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, _
    33         &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, _
    34         &H1, &HC0, &H80, &H41, &H1, &HC0, _
    35         &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, _
    36         &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, _
    37         &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, _
    38         &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, _
    39         &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, _
    40         &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, _
    41         &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, _
    42         &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, _
    43         &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, _
    44         &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40)
    45 End Function
    46 
    47 'CRC高位字节值表
    48 Function GetCRCHi(Ind As Long) As Byte
    49     GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, _
    50         &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, _
    51         &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, _
    52         &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, _
    53         &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, _
    54         &H13, &HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, _
    55         &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, _
    56         &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, _
    57         &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB, &H2B, &H2A, &HEA, &HEE, &H2E, _
    58         &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, _
    59         &HE6, &H26, &H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _
    60         &H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, _
    61         &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E, &HAE, &HAA, &H6A, _
    62         &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, _
    63         &H7A, &HBA, &HBE, &H7E, &H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, _
    64         &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, _
    65         &H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, _
    66         &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F, _
    67         &H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, _
    68         &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, _
    69         &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, _
    70         &H43, &H83, &H41, &H81, &H80, &H40)
    71 End Function
    72 
    73 Private Sub Form_Load()
    74     Dim d() As Byte
    75     d = StrConv("crc16test", vbFromUnicode) ' 字符串转字节数组
    76     Dim s() As Byte
    77     s = CRC16(d)
    78     ' s(0) 高字节 s(1) 低字节
    79     Me.AutoRedraw = True
    80     Me.Print Hex(s(0)) + Hex(s(1))  ' 显示出2481
    81 End Sub

    JavaScript

      1 var CRC = {};
      2 
      3 CRC._auchCRCHi = [
      4     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
      5     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
      6     0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
      7     0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
      8     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
      9     0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
     10     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
     11     0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
     12     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
     13     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
     14     0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
     15     0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
     16     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
     17     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
     18     0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
     19     0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
     20     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
     21     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
     22     0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
     23     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
     24     0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
     25     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
     26     0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
     27     0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
     28     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
     29     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
     30 ];
     31 CRC._auchCRCLo = [
     32     0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
     33     0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
     34     0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
     35     0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
     36     0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
     37     0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
     38     0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
     39     0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
     40     0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
     41     0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
     42     0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
     43     0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
     44     0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
     45     0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
     46     0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
     47     0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
     48     0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
     49     0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
     50     0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
     51     0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
     52     0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
     53     0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
     54     0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
     55     0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
     56     0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
     57     0x43, 0x83, 0x41, 0x81, 0x80, 0x40
     58 ];
     59 
     60 CRC.CRC16 = function (buffer) {
     61     var hi = 0xff;
     62     var lo = 0xff;
     63     for (var i = 0; i < buffer.length; i++) {
     64         var idx = hi ^ buffer[i];
     65         hi = (lo ^ CRC._auchCRCHi[idx]);
     66         lo = CRC._auchCRCLo[idx];
     67     }
     68     return CRC.padLeft((hi << 8 | lo).toString(16).toUpperCase(), 4, '0');
     69 };
     70 
     71 CRC.isArray = function (arr) {
     72     return Object.prototype.toString.call(arr) === '[object Array]';
     73 };
     74 
     75 CRC.ToCRC16 = function (str) {
     76     return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToByte(str));
     77 };
     78 
     79 CRC.ToModbusCRC16 = function (str) {
     80     return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToHex(str));
     81 };
     82 
     83 CRC.strToByte = function (str) {
     84     var tmp = str.split(''), arr = [];
     85     for (var i = 0, c = tmp.length; i < c; i++) {
     86         var j = encodeURI(tmp[i]);
     87         if (j.length == 1) {
     88             arr.push(j.charCodeAt());
     89         } else {
     90             var b = j.split('%');
     91             for (var m = 1; m < b.length; m++) {
     92                 arr.push(parseInt('0x' + b[m]));
     93             }
     94         }
     95     }
     96     return arr;
     97 };
     98 
     99 CRC.convertChinese = function (str) {
    100     var tmp = str.split(''), arr = [];
    101     for (var i = 0, c = tmp.length; i < c; i++) {
    102         var s = tmp[i].charCodeAt();
    103         if (s <= 0 || s >= 127) {
    104             arr.push(s.toString(16));
    105         }
    106         else {
    107             arr.push(tmp[i]);
    108         }
    109     }
    110     return arr;
    111 };
    112 
    113 CRC.filterChinese = function (str) {
    114     var tmp = str.split(''), arr = [];
    115     for (var i = 0, c = tmp.length; i < c; i++) {
    116         var s = tmp[i].charCodeAt();
    117         if (s > 0 && s < 127) {
    118             arr.push(tmp[i]);
    119         }
    120     }
    121     return arr;
    122 };
    123 
    124 CRC.strToHex = function (hex, isFilterChinese) {
    125     hex = isFilterChinese ? CRC.filterChinese(hex).join('') : CRC.convertChinese(hex).join('');
    126 
    127     //清除所有空格
    128     hex = hex.replace(/s/g, "");
    129     //若字符个数为奇数,补一个空格
    130     hex += hex.length % 2 != 0 ? " " : "";
    131 
    132     var c = hex.length / 2, arr = [];
    133     for (var i = 0; i < c; i++) {
    134         arr.push(parseInt(hex.substr(i * 2, 2), 16));
    135     }
    136     return arr;
    137 };
    138 
    139 CRC.padLeft = function (s, w, pc) {
    140     if (pc == undefined) {
    141         pc = '0';
    142     }
    143     for (var i = 0, c = w - s.length; i < c; i++) {
    144         s = pc + s;
    145     }
    146     return s;
    147 };
  • 相关阅读:
    [Tips] kubeconfig合并
    某书补充题选做
    Card
    Ant Design Pro V5 使用 Cesium 地图开发包遇到加载widgets.css样式报错
    工作相关
    pc端谷歌浏览器长截图
    dao层与xml之间联系
    三周的大学生活,我到底是怎么过来的
    湖南大学推荐书《社会学大纲》阅读有感 其二
    湖南大学新生报到游记 其一
  • 原文地址:https://www.cnblogs.com/yqmcu/p/11821655.html
Copyright © 2020-2023  润新知