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

释放双眼,带上耳机,听听看~!

C


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
11 #include <stdio.h>
2 2
3 3 #define u16 unsigned int
4 4 #define u8  unsigned char
5 5
6 6 u16 CRC16_Check(u8 *Pushdata,u16 length)
7 7 {
8 8     u16 Reg_CRC=0xffff;
9 9     u16 i,j;
1010
1111     for( i = 0; i<length; i ++)
1212     {
1313         Reg_CRC^= *Pushdata++;
1414
1515         for (j = 0; j<8; j++)
1616         {
1717             if (Reg_CRC & 0x0001){
1818                 Reg_CRC=Reg_CRC>>1^0xA001;
1919             }else{
2020                 Reg_CRC >>=1;
2121             }
2222         }
2323     }
2424     return (Reg_CRC);
2525 }
2626
2727
2828 int main(void)
2929 {
3030     printf("%d\n",sizeof("crc16test")-1); // length 9
3131     printf("%x\n",CRC16_Check((u8 *)"crc16test",sizeof("crc16test")-1)); // 2481
3232     return 0;
3333 }
34

Python


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

VB


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

JavaScript


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
11 var CRC = {};
2  2
3  3 CRC._auchCRCHi = [
4  4     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
5  5     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
6  6     0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
7  7     0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
8  8     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
9  9     0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
10 10     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
11 11     0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
12 12     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
13 13     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
14 14     0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
15 15     0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
16 16     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
17 17     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
18 18     0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
19 19     0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
20 20     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
21 21     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
22 22     0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
23 23     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
24 24     0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
25 25     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
26 26     0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
27 27     0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
28 28     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
29 29     0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
30 30 ];
31 31 CRC._auchCRCLo = [
32 32     0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
33 33     0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
34 34     0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
35 35     0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
36 36     0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
37 37     0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
38 38     0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
39 39     0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
40 40     0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
41 41     0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
42 42     0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
43 43     0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
44 44     0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
45 45     0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
46 46     0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
47 47     0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
48 48     0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
49 49     0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
50 50     0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
51 51     0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
52 52     0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
53 53     0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
54 54     0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
55 55     0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
56 56     0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
57 57     0x43, 0x83, 0x41, 0x81, 0x80, 0x40
58 58 ];
59 59
60 60 CRC.CRC16 = function (buffer) {
61 61     var hi = 0xff;
62 62     var lo = 0xff;
63 63     for (var i = 0; i < buffer.length; i++) {
64 64         var idx = hi ^ buffer[i];
65 65         hi = (lo ^ CRC._auchCRCHi[idx]);
66 66         lo = CRC._auchCRCLo[idx];
67 67     }
68 68     return CRC.padLeft((hi << 8 | lo).toString(16).toUpperCase(), 4, '0');
69 69 };
70 70
71 71 CRC.isArray = function (arr) {
72 72     return Object.prototype.toString.call(arr) === '[object Array]';
73 73 };
74 74
75 75 CRC.ToCRC16 = function (str) {
76 76     return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToByte(str));
77 77 };
78 78
79 79 CRC.ToModbusCRC16 = function (str) {
80 80     return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToHex(str));
81 81 };
82 82
83 83 CRC.strToByte = function (str) {
84 84     var tmp = str.split(''), arr = [];
85 85     for (var i = 0, c = tmp.length; i < c; i++) {
86 86         var j = encodeURI(tmp[i]);
87 87         if (j.length == 1) {
88 88             arr.push(j.charCodeAt());
89 89         } else {
90 90             var b = j.split('%');
91 91             for (var m = 1; m < b.length; m++) {
92 92                 arr.push(parseInt('0x' + b[m]));
93 93             }
94 94         }
95 95     }
96 96     return arr;
97 97 };
98 98
99 99 CRC.convertChinese = function (str) {
100100     var tmp = str.split(''), arr = [];
101101     for (var i = 0, c = tmp.length; i < c; i++) {
102102         var s = tmp[i].charCodeAt();
103103         if (s <= 0 || s >= 127) {
104104             arr.push(s.toString(16));
105105         }
106106         else {
107107             arr.push(tmp[i]);
108108         }
109109     }
110110     return arr;
111111 };
112112
113113 CRC.filterChinese = function (str) {
114114     var tmp = str.split(''), arr = [];
115115     for (var i = 0, c = tmp.length; i < c; i++) {
116116         var s = tmp[i].charCodeAt();
117117         if (s > 0 && s < 127) {
118118             arr.push(tmp[i]);
119119         }
120120     }
121121     return arr;
122122 };
123123
124124 CRC.strToHex = function (hex, isFilterChinese) {
125125     hex = isFilterChinese ? CRC.filterChinese(hex).join('') : CRC.convertChinese(hex).join('');
126126
127127     //清除所有空格
128128     hex = hex.replace(/\s/g, "");
129129     //若字符个数为奇数,补一个空格
130130     hex += hex.length % 2 != 0 ? " " : "";
131131
132132     var c = hex.length / 2, arr = [];
133133     for (var i = 0; i < c; i++) {
134134         arr.push(parseInt(hex.substr(i * 2, 2), 16));
135135     }
136136     return arr;
137137 };
138138
139139 CRC.padLeft = function (s, w, pc) {
140140     if (pc == undefined) {
141141         pc = '0';
142142     }
143143     for (var i = 0, c = w - s.length; i < c; i++) {
144144         s = pc + s;
145145     }
146146     return s;
147147 };
148

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ vector

2022-1-11 12:36:11

安全运维

基于深度学习的推荐(五):CTR预测经典模型PNN

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索