上饶市公安局wvp平台
zhongrj
2023-03-07 9f8d7e3f8eb1e81e6eab411a415030085ff64da5
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.genersoft.iot.vmp.netty.util;
 
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
public class HexStringTool {
 
 
    private static final char[] HEX_CHAR_ARR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
    private static final String HEX_STR = "0123456789abcdef";
 
    //对字符串,进行分组,转化为List
    public static List<String> getStrList(String str16, int frontLen, int size, int length) {
        List<String> list = new ArrayList<String>();
        String content = str16.substring(frontLen);
        String tem = "";
        for (int index = 0; index < size; index++) {
            tem = content.substring(0, length);
            content = content.substring(length);
            list.add(tem);
        }
        return list;
    }
 
    //对字符串,进行分组,转化为List(字符串数据类型转化专用)
    public static List<String> getStrList(String str16, int frontLen, int size, int totalLen, int lenLen) {
        List<String> list = new ArrayList<>();
        String content = str16.substring(frontLen);
        String tem = "";
        for (int index = 0; index < size; index++) {
            String len16 = content.substring(totalLen * 2, (totalLen + lenLen) * 2);
            int len = Integer.parseInt(len16, 16);
            int length = (totalLen + lenLen + len) * 2;
            tem = content.substring(0, length);
            content = content.substring(length);
            list.add(tem);
        }
        return list;
    }
 
    //字符串转换unicode
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }
 
    //字符串转化成为16进制字符串
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }
 
    //16进制转换成为string类型字符串
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
 
    //unicode 转字符串
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }
 
    //字符串转换成为16进制(无需Unicode编码)
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }
 
    //16进制直接转换成为字符串(无需Unicode解码)
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }
 
    /**
     * int/long转16字节字符串(按所占字节数用0补齐)
     *
     * @param byteLen 所占字节长度
     * @param b       需要转换的数字
     * @return str16
     */
    public static String numToStr16(int byteLen, long b) {
        return String.format("%0" + byteLen * 2 + "x", b);
    }
 
    /**
     * float/double转16字节字符串(按所占字节数用0补齐)
     *
     * @param byteLen 所占字节长度
     * @param b       需要转换的数字
     * @return str16
     */
    public static String numToStr16(int byteLen, double b) {
        byte[] bytes = Hex.getBytes(b);
        int anInt = Hex.getInt(bytes);
        return String.format("%0" + byteLen * 2 + "x", anInt);
    }
 
    /**
     * 16进制字符串转字符串(ASCII)
     *
     * @param x16 十六进制字符串
     * @return string
     */
    public static String x16toString(String x16) {
        if (x16 == null || "".equals(x16.trim())) {
            return "";
        }
        String tempStr = "";
        byte[] b = new byte[x16.length() / 2];
        for (int i = 0; i < x16.length() / 2; i++) {
            tempStr = x16.substring(i * 2, i * 2 + 2);
            int temp = Integer.parseInt(tempStr, 16);
            b[i] = (byte) temp;
        }
        return new String(b, StandardCharsets.US_ASCII);
    }
 
    /**
     * 16进制字符串转字符串(GBK)
     *
     * @param x16 十六进制字符串
     * @return string
     */
    public static String x16toGbk(String x16) {
        byte[] bytes = new byte[x16.length() / 2];
        byte tempByte = 0;
        byte tempHigh = 0;
        byte tempLow = 0;
        for (int i = 0, j = 0; i < x16.length(); i += 2, j++) {
            tempByte = (byte) (((int) x16.charAt(i)) & 0xff);
            if (tempByte >= 48 && tempByte <= 57) {
                tempHigh = (byte) ((tempByte - 48) << 4);
            } else if (tempByte >= 97 && tempByte <= 101) {
                tempHigh = (byte) ((tempByte - 97 + 10) << 4);
            }
            tempByte = (byte) (((int) x16.charAt(i + 1)) & 0xff);
            if (tempByte >= 48 && tempByte <= 57) {
                tempLow = (byte) (tempByte - 48);
            } else if (tempByte >= 97 && tempByte <= 101) {
                tempLow = (byte) (tempByte - 97 + 10);
            }
            bytes[j] = (byte) (tempHigh | tempLow);
        }
        String result = null;
        try {
            result = new String(bytes, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
 
    public static boolean isChinese(String s) {
        boolean result = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
            if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                    || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                    || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                    || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
                result = true;
            }
        }
        return result;
    }
 
    private static String byteArrToHex(byte[] btArr) {
        char[] strArr = new char[btArr.length * 2];
        int i = 0;
        for (byte bt : btArr) {
            strArr[i++] = HEX_CHAR_ARR[bt >>> 4 & 0xf];
            strArr[i++] = HEX_CHAR_ARR[bt & 0xf];
        }
        return new String(strArr);
    }
 
    private static byte[] hexToByteArr(String hexStr) {
        char[] charArr = hexStr.toCharArray();
        byte[] btArr = new byte[charArr.length / 2];
        int index = 0;
        for (int i = 0; i < charArr.length; i++) {
            int highBit = HEX_STR.indexOf(charArr[i]);
            int lowBit = HEX_STR.indexOf(charArr[++i]);
            btArr[index] = (byte) (highBit << 4 | lowBit);
            index++;
        }
        return btArr;
    }
 
    /**
     * 字符串转16进制字符串(无汉字utf-8,有汉字gbk)
     *
     * @param str
     * @return
     */
    public static String str2StrHex(String str) {
        boolean chinese = isChinese(str);
        if (chinese) {
            try {
                byte[] bytes1 = str.getBytes(StandardCharsets.UTF_8);
                byte[] bytes2 = new String(bytes1, StandardCharsets.UTF_8).getBytes("gbk");
                String gbk = new String(bytes2, "gbk");
                byte[] gbkArr = gbk.getBytes();
                return byteArrToHex(gbkArr);
 
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return strTo16(str);
        }
    }
 
    /**
     * 16进制字符串转字符串(无汉字utf-8,有汉字gbk)
     *
     * @param strHex
     * @return
     */
    public static String strHex2Str(String strHex) {
        byte[] bytes = hexToByteArr(strHex);
        String s1 = new String(bytes, StandardCharsets.UTF_8);
        boolean chinese = isChinese(s1);
        if (!chinese) {
            try {
                return new String(bytes, "gbk");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return s1;
        }
    }
 
 
}