jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.protocol.utl;
 
 
import java.text.ParseException;
 
/**
 * GPS工具类
 */
public class GpsUtils {
 
    /**
     * 标识位
     */
    public static final String MSG = "7E";
    /**
     * 注册消息ID
     */
    public static final String MSG_01 = "0100";
    /**
     * 注册应答消息ID
     */
    public static final String MSG_11 = "8100";
    /**
     * 鉴权消息ID
     */
    public static final String MSG_02 = "0102";
    /**
     * 鉴权应答消息ID
     */
    public static final String MSG_22 = "8001";
    /**
     * 位置信息主动汇报消息ID
     */
    public static final String MSG_333 = "0200";
    /**
     * 位置信息查询应答消息ID
     */
    public static final String MSG_03 = "0201";
    /**
     * 位置信息查询消息ID
     */
    public static final String MSG_33 = "8201";
 
    /**
     * 添加标识符
     * @param msg
     * @return
     */
    public static String addIdentifier(String msg){
        return MSG+msg+MSG;
    }
 
    /**
     * 去除标识符
     * @param msg
     * @return
     */
    public static String delIdentifier(String msg){
        return msg.replaceAll(MSG,"");
    }
 
    /**
     * 转义:0x7D-->0x7D0x01,0x7E-->0x7D0x02
     * @param msg
     * @return
     */
    public static String escapeCode1(String msg){
        String str = "";
        for(int i=0;i<=msg.length()-2;i=i+2){
            str += "0x" + msg.substring(i,i+2);
        }
        String s = str.replaceAll("0x7D", "0x7D0x01");
        return s.replaceAll("0x7E", "0x7D0x02").replaceAll("0x", "");
    }
 
    /**
     * 转义:0x7D0x02-->0x7E,0x7D0x01-->0x7D
     * @param msg
     * @return
     */
    public static String escapeCode2(String msg){
        String str = "";
        for(int i=0;i<=msg.length()-2;i=i+2){
            str += "0x" + msg.substring(i,i+2);
        }
        String s = str.replaceAll("0x7D0x02", "0x7E");
        return s.replaceAll("0x7D0x01", "0x7D").replaceAll("0x", "");
    }
 
    public static void main(String[] args) throws ParseException {
        String msg = "01 02 00 06 01 38 20 03 63 16 00 01 01 38 20 03 63 16";
        String checkCode = checkCode(msg.replaceAll(" ",""));
        System.out.println(checkCode);
    }
 
    /**
     * BCC校验(异或校验)
     * @param content
     * @return
     */
    public static String checkCode(String content) {
        String con = change(content);
        String[] b = con.split(" ");
        int a = 0;
        for (int i = 0; i < b.length; i++) {
            a = a ^ Integer.parseInt(b[i], 16);
        }
        if(a<10){
            StringBuffer sb = new StringBuffer();
            sb.append("0");
            sb.append(a);
            return sb.toString();
        }
        return Integer.toHexString(a);
    }
    private static String change(String content) {
        String str = "";
        for (int i = 0; i < content.length(); i++) {
            if (i % 2 == 0) {
                str += " " + content.substring(i, i + 1);
            } else {
                str += content.substring(i, i + 1);
            }
        }
        return str.trim();
    }
}