czt
2024-07-13 1c1472f51d68382f913e413aa827fd0b028ab589
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.ld.igds.protocol.zldz.util;
 
/**
 *计算CRC8校验值
 */
public class CRC8 {
 
    public static void main(String[] args) {
 
    }
 
    static short[] gCrc16Table = { (short) 0x0000, (short) 0x1021, (short) 0x2042, (short) 0x3063, (short) 0x4084,
            (short) 0x50a5, (short) 0x60c6, (short) 0x70e7, (short) 0xd94c, (short) 0xc96d, (short) 0xf90e,
            (short) 0xe92f, (short) 0x99c8, (short) 0x89e9, (short) 0xb98a, (short) 0xa9ab, (short) 0x5844,
            (short) 0x4865, (short) 0x7806, (short) 0x6827, (short) 0x18c0, (short) 0x08e1, (short) 0x3882,
            (short) 0x28a3, (short) 0xcb7d, (short) 0xdb5c, (short) 0xeb3f, (short) 0xfb1e, (short) 0x8bf9,
            (short) 0x9bd8, (short) 0xabbb, (short) 0xbb9a, (short) 0x4a75, (short) 0x5a54, (short) 0x6a37,
            (short) 0x7a16, (short) 0x0af1, (short) 0x1ad0, (short) 0x2ab3, (short) 0x3a92, (short) 0xfd2e,
            (short) 0xed0f, (short) 0xdd6c, (short) 0xcd4d, (short) 0xbdaa, (short) 0xad8b, (short) 0x9de8,
            (short) 0x8dc9, (short) 0xc9cc, (short) 0xd9ed, (short) 0xe98e, (short) 0xf9af, (short) 0x8948,
            (short) 0x9969, (short) 0xa90a, (short) 0xb92b, (short) 0x5af5, (short) 0x4ad4, (short) 0x7ab7,
            (short) 0x6a96, (short) 0x1a71, (short) 0x0a50, (short) 0x3a33, (short) 0x2a12, (short) 0xdbfd,
            (short) 0xcbdc, (short) 0xfbbf, (short) 0xeb9e, (short) 0x9b79, (short) 0x8b58, (short) 0xbb3b,
            (short) 0xab1a, (short) 0x6ca6, (short) 0x7c87, (short) 0x4ce4, (short) 0x5cc5, (short) 0x2c22,
            (short) 0x3c03, (short) 0x0c60, (short) 0x1c41, (short) 0x8108, (short) 0x9129, (short) 0xa14a,
            (short) 0xb16b, (short) 0xc18c, (short) 0xd1ad, (short) 0xe1ce, (short) 0xf1ef, (short) 0x1231,
            (short) 0x0210, (short) 0x3273, (short) 0x2252, (short) 0x52b5, (short) 0x4294, (short) 0x72f7,
            (short) 0x62d6, (short) 0x9339, (short) 0x8318, (short) 0xb37b, (short) 0xa35a, (short) 0xd3bd,
            (short) 0xc39c, (short) 0xf3ff, (short) 0xe3de, (short) 0x2462, (short) 0x3443, (short) 0x0420,
            (short) 0x1401, (short) 0x64e6, (short) 0x74c7, (short) 0x44a4, (short) 0x5485, (short) 0xa56a,
            (short) 0xb54b, (short) 0x8528, (short) 0x9509, (short) 0xe5ee, (short) 0xf5cf, (short) 0xc5ac,
            (short) 0xd58d, (short) 0x3653, (short) 0x2672, (short) 0x1611, (short) 0x0630, (short) 0x76d7,
            (short) 0x66f6, (short) 0x5695, (short) 0x46b4, (short) 0xb75b, (short) 0xa77a, (short) 0x9719,
            (short) 0x8738, (short) 0xf7df, (short) 0xe7fe, (short) 0xd79d, (short) 0xc7bc, (short) 0x48c4,
            (short) 0x58e5, (short) 0x6886, (short) 0x78a7, (short) 0x0840, (short) 0x1861, (short) 0x2802,
            (short) 0x3823, (short) 0x83b9, (short) 0x9398, (short) 0xa3fb, (short) 0xb3da, (short) 0xc33d,
            (short) 0xd31c, (short) 0xe37f, (short) 0xf35e, (short) 0x02b1, (short) 0x1290, (short) 0x22f3,
            (short) 0x32d2, (short) 0x4235, (short) 0x5214, (short) 0x6277, (short) 0x7256, (short) 0xb5ea,
            (short) 0xa5cb, (short) 0x95a8, (short) 0x8589, (short) 0xf56e, (short) 0xe54f, (short) 0xd52c,
            (short) 0xc50d, (short) 0x34e2, (short) 0x24c3, (short) 0x14a0, (short) 0x0481, (short) 0x7466,
            (short) 0x6447, (short) 0x5424, (short) 0x4405, (short) 0xa7db, (short) 0xb7fa, (short) 0x8799,
            (short) 0x97b8, (short) 0xe75f, (short) 0xf77e, (short) 0xc71d, (short) 0xd73c, (short) 0x26d3,
            (short) 0x36f2, (short) 0x0691, (short) 0x16b0, (short) 0x6657, (short) 0x7676, (short) 0x4615,
            (short) 0x5634, (short) 0xedae, (short) 0xfd8f, (short) 0xcdec, (short) 0xddcd, (short) 0xad2a,
            (short) 0xbd0b, (short) 0x8d68, (short) 0x9d49, (short) 0x7e97, (short) 0x6eb6, (short) 0x5ed5,
            (short) 0x4ef4, (short) 0x3e13, (short) 0x2e32, (short) 0x1e51, (short) 0x0e70, (short) 0xff9f,
            (short) 0xefbe, (short) 0xdfdd, (short) 0xcffc, (short) 0xbf1b, (short) 0xaf3a, (short) 0x9f59,
            (short) 0x8f78, (short) 0x9188, (short) 0x81a9, (short) 0xb1ca, (short) 0xa1eb, (short) 0xd10c,
            (short) 0xc12d, (short) 0xf14e, (short) 0xe16f, (short) 0x1080, (short) 0x00a1, (short) 0x30c2,
            (short) 0x20e3, (short) 0x5004, (short) 0x4025, (short) 0x7046, (short) 0x6067, (short) 0x7c26,
            (short) 0x6c07, (short) 0x5c64, (short) 0x4c45, (short) 0x3ca2, (short) 0x2c83, (short) 0x1ce0,
            (short) 0x0cc1, (short) 0xef1f, (short) 0xff3e, (short) 0xcf5d, (short) 0xdf7c, (short) 0xaf9b,
            (short) 0xbfba, (short) 0x8fd9, (short) 0x9ff8, (short) 0x6e17, (short) 0x7e36, (short) 0x4e55,
            (short) 0x5e74, (short) 0x2e93, (short) 0x3eb2, (short) 0x0ed1, (short) 0x1ef0 };
 
    static short[] crc8_tab = { (short) 0x00, (short) 0x07, (short) 0x0E, (short) 0x09, (short) 0x1C, (short) 0x1B,
            (short) 0x12, (short) 0x15, (short) 0x38, (short) 0x3F, (short) 0x36, (short) 0x31, (short) 0x24,
            (short) 0x23, (short) 0x2A, (short) 0x2D, (short) 0x70, (short) 0x77, (short) 0x7E, (short) 0x79,
            (short) 0x6C, (short) 0x6B, (short) 0x62, (short) 0x65, (short) 0x48, (short) 0x4F, (short) 0x46,
            (short) 0x41, (short) 0x54, (short) 0x53, (short) 0x5A, (short) 0x5D, (short) 0xE0, (short) 0xE7,
            (short) 0xEE, (short) 0xE9, (short) 0xFC, (short) 0xFB, (short) 0xF2, (short) 0xF5, (short) 0xD8,
            (short) 0xDF, (short) 0xD6, (short) 0xD1, (short) 0xC4, (short) 0xC3, (short) 0xCA, (short) 0xCD,
            (short) 0x90, (short) 0x97, (short) 0x9E, (short) 0x99, (short) 0x8C, (short) 0x8B, (short) 0x82,
            (short) 0x85, (short) 0xA8, (short) 0xAF, (short) 0xA6, (short) 0xA1, (short) 0xB4, (short) 0xB3,
            (short) 0xBA, (short) 0xBD, (short) 0xC7, (short) 0xC0, (short) 0xC9, (short) 0xCE, (short) 0xDB,
            (short) 0xDC, (short) 0xD5, (short) 0xD2, (short) 0xFF, (short) 0xF8, (short) 0xF1, (short) 0xF6,
            (short) 0xE3, (short) 0xE4, (short) 0xED, (short) 0xEA, (short) 0xB7, (short) 0xB0, (short) 0xB9,
            (short) 0xBE, (short) 0xAB, (short) 0xAC, (short) 0xA5, (short) 0xA2, (short) 0x8F, (short) 0x88,
            (short) 0x81, (short) 0x86, (short) 0x93, (short) 0x94, (short) 0x9D, (short) 0x9A, (short) 0x27,
            (short) 0x20, (short) 0x29, (short) 0x2E, (short) 0x3B, (short) 0x3C, (short) 0x35, (short) 0x32,
            (short) 0x1F, (short) 0x18, (short) 0x11, (short) 0x16, (short) 0x03, (short) 0x04, (short) 0x0D,
            (short) 0x0A, (short) 0x57, (short) 0x50, (short) 0x59, (short) 0x5E, (short) 0x4B, (short) 0x4C,
            (short) 0x45, (short) 0x42, (short) 0x6F, (short) 0x68, (short) 0x61, (short) 0x66, (short) 0x73,
            (short) 0x74, (short) 0x7D, (short) 0x7A, (short) 0x89, (short) 0x8E, (short) 0x87, (short) 0x80,
            (short) 0x95, (short) 0x92, (short) 0x9B, (short) 0x9C, (short) 0xB1, (short) 0xB6, (short) 0xBF,
            (short) 0xB8, (short) 0xAD, (short) 0xAA, (short) 0xA3, (short) 0xA4, (short) 0xF9, (short) 0xFE,
            (short) 0xF7, (short) 0xF0, (short) 0xE5, (short) 0xE2, (short) 0xEB, (short) 0xEC, (short) 0xC1,
            (short) 0xC6, (short) 0xCF, (short) 0xC8, (short) 0xDD, (short) 0xDA, (short) 0xD3, (short) 0xD4,
            (short) 0x69, (short) 0x6E, (short) 0x67, (short) 0x60, (short) 0x75, (short) 0x72, (short) 0x7B,
            (short) 0x7C, (short) 0x51, (short) 0x56, (short) 0x5F, (short) 0x58, (short) 0x4D, (short) 0x4A,
            (short) 0x43, (short) 0x44, (short) 0x19, (short) 0x1E, (short) 0x17, (short) 0x10, (short) 0x05,
            (short) 0x02, (short) 0x0B, (short) 0x0C, (short) 0x21, (short) 0x26, (short) 0x2F, (short) 0x28,
            (short) 0x3D, (short) 0x3A, (short) 0x33, (short) 0x34, (short) 0x4E, (short) 0x49, (short) 0x40,
            (short) 0x47, (short) 0x52, (short) 0x55, (short) 0x5C, (short) 0x5B, (short) 0x76, (short) 0x71,
            (short) 0x78, (short) 0x7F, (short) 0x6A, (short) 0x6D, (short) 0x64, (short) 0x63, (short) 0x3E,
            (short) 0x39, (short) 0x30, (short) 0x37, (short) 0x22, (short) 0x25, (short) 0x2C, (short) 0x2B,
            (short) 0x06, (short) 0x01, (short) 0x08, (short) 0x0F, (short) 0x1A, (short) 0x1D, (short) 0x14,
            (short) 0x13, (short) 0xAE, (short) 0xA9, (short) 0xA0, (short) 0xA7, (short) 0xB2, (short) 0xB5,
            (short) 0xBC, (short) 0xBB, (short) 0x96, (short) 0x91, (short) 0x98, (short) 0x9F, (short) 0x8A,
            (short) 0x8D, (short) 0x84, (short) 0x83, (short) 0xDE, (short) 0xD9, (short) 0xD0, (short) 0xD7,
            (short) 0xC2, (short) 0xC5, (short) 0xCC, (short) 0xCB, (short) 0xE6, (short) 0xE1, (short) 0xE8,
            (short) 0xEF, (short) 0xFA, (short) 0xFD, (short) 0xF4, (short) 0xF3 };
 
    static byte[] crc8_tab2 = { (byte) 0x00, (byte) 0x07, (byte) 0x0E, (byte) 0x09, (byte) 0x1C, (byte) 0x1B,
            (byte) 0x12, (byte) 0x15, (byte) 0x38, (byte) 0x3F, (byte) 0x36, (byte) 0x31, (byte) 0x24, (byte) 0x23,
            (byte) 0x2A, (byte) 0x2D, (byte) 0x70, (byte) 0x77, (byte) 0x7E, (byte) 0x79, (byte) 0x6C, (byte) 0x6B,
            (byte) 0x62, (byte) 0x65, (byte) 0x48, (byte) 0x4F, (byte) 0x46, (byte) 0x41, (byte) 0x54, (byte) 0x53,
            (byte) 0x5A, (byte) 0x5D, (byte) 0xE0, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xFC, (byte) 0xFB,
            (byte) 0xF2, (byte) 0xF5, (byte) 0xD8, (byte) 0xDF, (byte) 0xD6, (byte) 0xD1, (byte) 0xC4, (byte) 0xC3,
            (byte) 0xCA, (byte) 0xCD, (byte) 0x90, (byte) 0x97, (byte) 0x9E, (byte) 0x99, (byte) 0x8C, (byte) 0x8B,
            (byte) 0x82, (byte) 0x85, (byte) 0xA8, (byte) 0xAF, (byte) 0xA6, (byte) 0xA1, (byte) 0xB4, (byte) 0xB3,
            (byte) 0xBA, (byte) 0xBD, (byte) 0xC7, (byte) 0xC0, (byte) 0xC9, (byte) 0xCE, (byte) 0xDB, (byte) 0xDC,
            (byte) 0xD5, (byte) 0xD2, (byte) 0xFF, (byte) 0xF8, (byte) 0xF1, (byte) 0xF6, (byte) 0xE3, (byte) 0xE4,
            (byte) 0xED, (byte) 0xEA, (byte) 0xB7, (byte) 0xB0, (byte) 0xB9, (byte) 0xBE, (byte) 0xAB, (byte) 0xAC,
            (byte) 0xA5, (byte) 0xA2, (byte) 0x8F, (byte) 0x88, (byte) 0x81, (byte) 0x86, (byte) 0x93, (byte) 0x94,
            (byte) 0x9D, (byte) 0x9A, (byte) 0x27, (byte) 0x20, (byte) 0x29, (byte) 0x2E, (byte) 0x3B, (byte) 0x3C,
            (byte) 0x35, (byte) 0x32, (byte) 0x1F, (byte) 0x18, (byte) 0x11, (byte) 0x16, (byte) 0x03, (byte) 0x04,
            (byte) 0x0D, (byte) 0x0A, (byte) 0x57, (byte) 0x50, (byte) 0x59, (byte) 0x5E, (byte) 0x4B, (byte) 0x4C,
            (byte) 0x45, (byte) 0x42, (byte) 0x6F, (byte) 0x68, (byte) 0x61, (byte) 0x66, (byte) 0x73, (byte) 0x74,
            (byte) 0x7D, (byte) 0x7A, (byte) 0x89, (byte) 0x8E, (byte) 0x87, (byte) 0x80, (byte) 0x95, (byte) 0x92,
            (byte) 0x9B, (byte) 0x9C, (byte) 0xB1, (byte) 0xB6, (byte) 0xBF, (byte) 0xB8, (byte) 0xAD, (byte) 0xAA,
            (byte) 0xA3, (byte) 0xA4, (byte) 0xF9, (byte) 0xFE, (byte) 0xF7, (byte) 0xF0, (byte) 0xE5, (byte) 0xE2,
            (byte) 0xEB, (byte) 0xEC, (byte) 0xC1, (byte) 0xC6, (byte) 0xCF, (byte) 0xC8, (byte) 0xDD, (byte) 0xDA,
            (byte) 0xD3, (byte) 0xD4, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x60, (byte) 0x75, (byte) 0x72,
            (byte) 0x7B, (byte) 0x7C, (byte) 0x51, (byte) 0x56, (byte) 0x5F, (byte) 0x58, (byte) 0x4D, (byte) 0x4A,
            (byte) 0x43, (byte) 0x44, (byte) 0x19, (byte) 0x1E, (byte) 0x17, (byte) 0x10, (byte) 0x05, (byte) 0x02,
            (byte) 0x0B, (byte) 0x0C, (byte) 0x21, (byte) 0x26, (byte) 0x2F, (byte) 0x28, (byte) 0x3D, (byte) 0x3A,
            (byte) 0x33, (byte) 0x34, (byte) 0x4E, (byte) 0x49, (byte) 0x40, (byte) 0x47, (byte) 0x52, (byte) 0x55,
            (byte) 0x5C, (byte) 0x5B, (byte) 0x76, (byte) 0x71, (byte) 0x78, (byte) 0x7F, (byte) 0x6A, (byte) 0x6D,
            (byte) 0x64, (byte) 0x63, (byte) 0x3E, (byte) 0x39, (byte) 0x30, (byte) 0x37, (byte) 0x22, (byte) 0x25,
            (byte) 0x2C, (byte) 0x2B, (byte) 0x06, (byte) 0x01, (byte) 0x08, (byte) 0x0F, (byte) 0x1A, (byte) 0x1D,
            (byte) 0x14, (byte) 0x13, (byte) 0xAE, (byte) 0xA9, (byte) 0xA0, (byte) 0xA7, (byte) 0xB2, (byte) 0xB5,
            (byte) 0xBC, (byte) 0xBB, (byte) 0x96, (byte) 0x91, (byte) 0x98, (byte) 0x9F, (byte) 0x8A, (byte) 0x8D,
            (byte) 0x84, (byte) 0x83, (byte) 0xDE, (byte) 0xD9, (byte) 0xD0, (byte) 0xD7, (byte) 0xC2, (byte) 0xC5,
            (byte) 0xCC, (byte) 0xCB, (byte) 0xE6, (byte) 0xE1, (byte) 0xE8, (byte) 0xEF, (byte) 0xFA, (byte) 0xFD,
            (byte) 0xF4, (byte) 0xF3 };
 
    /**
     * 计算数组的CRC8校验值
     * 
     * @param data
     *            需要计算的数组
     * @return CRC8校验值
     */
    public static byte calcCrc8(byte[] data) {
        return calcCrc8(data, (short) 2, (short) (data.length - 2), (byte) 0);
    }
 
    /**
     * 计算CRC8校验值
     * 
     * @param data
     *            数据
     * @param offset
     *            起始位置
     * @param len
     *            长度
     * @return 校验值
     */
    public static byte calcCrc8(byte[] data, short offset, short len) {
        return calcCrc8(data, offset, len, (byte) 0);
    }
 
    public static byte calcCrc8(byte[] ucPtr, short offset, short ucLen, byte preval) {
        short ucIndex; // CRC8校验表格索引
        short ucCRC8 = 0; // CRC8字节初始化
        short i = 0;
        // 进行CRC8位校验
        while ((ucLen--) > 0) {
            short indexI = (short) (i + offset);
            short data = ucPtr[indexI];
            if (data < 0) {
                int sd = Byte.toUnsignedInt(ucPtr[indexI]);
                data = (short) sd;
            }
            ucIndex = (short) (ucCRC8 ^ data);
            ucCRC8 = crc8_tab[(short) ucIndex];
            i++;
        }
        // 返回CRC8校验数据
        return (byte) (~ucCRC8);
    }
 
    public static short crc16Byte(short crc, byte data) {
        return (short) ((crc << 8) ^ gCrc16Table[((crc >> 8) ^ data) & 0xff]);
    }
 
    public static short calculateCrc16(byte[] buf, short offset, short length) {
        short crc = 0;
        short i = 0;
        while ((length--) > 0) {
            crc = crc16Byte(crc, buf[offset + (i++)]);
        }
        return crc;
    }
}