cjl
2023-07-27 7e5452d35bb30dad5bc9e37360047b5a43732dcd
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
package com.moral.api.config.rest;
 
public class Crc16Utils {
 
    public static String crc16_2017(String pushMsg, int usDataLen) {
        int crc_reg=0xFFFF;
        int check;
        for(int i =0; i<usDataLen;i++){
            crc_reg = (crc_reg>>8) ^ pushMsg.charAt(i);
            for(int j =0; j<8; j++){
                check = crc_reg & 0x0001;
                crc_reg >>= 1;
                if(check == 0x0001){
                    crc_reg ^= 0xA001;
                }
            }
        }
        return padLeft(Integer.toHexString(crc_reg).toUpperCase(), 4, "0");
    }
 
    public static String padLeft(String s, int w, String pc) {
        if (pc == null) {
            pc = "0";
        }
        for (int i = 0, c = w - s.length(); i < c; i++) {
            s = pc + s;
        }
        return s;
    }
 
 
    public static String padLeftTest(String s, int w, String pc) {
        if (pc == null) {
            pc = "0";
        }
        for (int i = 0, c = w - s.length(); i < c; i++) {
            s = pc + s;
        }
        return s;
    }
}