2021与蓝度共同重构项目,服务端
liuhaonan
2022-02-14 0d75b97f3dc35013f1b6dca1b639e16275bfc95d
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.sandu.ximon.admin.utils;
 
import java.util.ArrayList;
 
/**
 * 16
 */
public class HexStrConvertUtil {
    /**
     * byte数组转换二进制字符串
     **/
    public static String HexToByteStr(String hexStr) {
        String[] strs = new String[]{
                "0000","0001","0010","0011",
                "0100","0101","0110","0111",
                "1000","1001","1010","1011",
                "1100","1101","1110","1111"
        };
        String result = "";
        for(char c:hexStr.toCharArray()){
            result = result + strs["0123456789ABCDEF".indexOf(c)];
        }
 
        return result;
    }
 
    /**
     * 字符串转换为16进制字符串
     *
     * @param s
     * @return
     */
    public static String stringToHexString(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }
 
    /**
     * 16进制转换成为string类型字符串
     * @param s
     * @return
     */
    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;
    }
 
 
    /**
     * @param: [content]
     * @return: int
     * @description: 十六进制转十进制
     */
//    public static int HexToInt(String content){
//        int number=0;
//        String [] HighLetter = {"A","B","C","D","E","F"};
//        Map<String,Integer> map = new HashMap<>();
//        for(int i = 0;i <= 9;i++){
//            map.put(i+"",i);
//        }
//        for(int j= 10;j<HighLetter.length+10;j++){
//            map.put(HighLetter[j-10],j);
//        }
//        String[]str = new String[content.length()];
//        for(int i = 0; i < str.length; i++){
//            str[i] = content.substring(i,i+1);
//        }
//        for(int i = 0; i < str.length; i++){
//            number += map.get(str[i])*Math.pow(16,str.length-1-i);
//        }
//        return number;
//    }
 
 
    /**
     * @param bArray
     * @return HexString
     */
    public static final ArrayList bytesToHexList(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                arrayList.add(0 + sTemp);
            } else {
                arrayList.add(sTemp);
            }
        }
        return arrayList;
    }
 
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toLowerCase());
        }
        return sb.toString();
    }
 
    public static final String byteToHexString(byte b) {
        StringBuffer sb = new StringBuffer();
        String sTemp;
        sTemp = Integer.toHexString(0xFF & b);
        if (sTemp.length() < 2) {
            sb.append(0);
        }
        sb.append(sTemp.toLowerCase());
        return sb.toString();
    }
 
    /**
     * 十进制转换成十六进制
     */
    public static String tenToHex(int t) {
        return Integer.toHexString(t).toUpperCase();
    }
 
    /**
     * @param src String
     * @return byte[]
     **/
    public static byte[] hexStringToByt (String src) {
        src = src.replaceAll(" ", "");
//        "\"19 03 00 00 00 0b 07 d5\""
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < src.length() / 2; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }
 
    public static byte[] hexStringToByteArray (String s) {
 
        int len = s.length();
        byte[] data = new byte[len/2];
 
        for(int i = 0; i < len+1; i+=2){
            data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
        }
 
        return data;
    }
 
    public static byte[] hexToByteArrays(String hex) {
 
        hex = hex.length()%2 != 0?"0"+hex:hex;
 
        byte[] b = new byte[hex.length() / 2];
 
        for (int i = 0; i < b.length; i++) {
            int index = i * 2;
            int v = Integer.parseInt(hex.substring(index, index + 2), 16);
            b[i] = (byte) v;
        }
        return b;
    }
 
 
 
    /** 十六进制转换成字节数组 */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase(); // 十六进制转大写字母
        int length = hexString.length() / 2; // 获取十六进制的长度,2个字符为一个十六进制
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    /** char转byte */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
//    /**
//     * 把16进制字符串转换成字节数组
//     *
//     * @param hex
//     * @return
//     */
//    public static byte[] hexStringToByte(String hex) {
//        int len = (hex.length() / 2);
//        byte[] result = new byte[len];
//        char[] achar = hex.toCharArray();
//        for (int i = 0; i < len; i++) {
//            int pos = i * 2;
//            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
//        }
//        return result;
//    }
 
    /**
     * 16进制字符串 转换为对应的 byte数组
     */
    public static byte[] hex2Bytes(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }
 
        char[] hexChars = hex.toCharArray();
        byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个
 
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
        }
        return bytes;
    }
 
 
    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
 
    /**
     * hex字符串转byte数组
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex){
 
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1){
            //奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {
            //偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2){
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }
 
 
    /**
     * Hex字符串转byte
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte
     */
    public static byte hexToByte(String inHex){
        return (byte)Integer.parseInt(inHex,16);
    }
 
    public static byte[] hexStr2Bytes(String src) {
        int m = 0, n = 0;
        int l = src.length() / 2;
        System.out.println(l);
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
        }
        return ret;
    }
 
    public static byte[] bytes2HexByte(byte[] b) {
        byte[] result = new byte[b.length];
        String hex;
        for (int i = 0; i < b.length; i++) {
            hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            result[i] = Byte.parseByte(hex);
        }
        return result;
    }
 
    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        System.out.print(ret);
        return ret;
    }
 
    public static boolean isHex(String str) {
        boolean isHexFlg = true;
        int i = 0;
        char c;
        for (i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')) || (c == ' '))) {
                isHexFlg = false;
                break;
            }
        }
        return isHexFlg;
    }
 
 
//    public static byte[] readInputStream(InputStream inputStream) {
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//
//        if (inputStream != null) {
//            try {
//                byte[] buffer = new byte[91];
//                int counts;
//                while ((counts = inputStream.read(buffer, 0, buffer.length)) > 0) {
//                    byteArrayOutputStream.write(buffer, 0, counts);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }/* finally {
//                if (byteArrayOutputStream != null) {
//                    try {
//                        byteArrayOutputStream.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }*/
//        }
//        return byteArrayOutputStream.toByteArray();
//    }
 
    //                br = new BufferedReader(new InputStreamReader(inputStream));
//                byteArrayOutputStream = new ByteArrayOutputStream();
//                while ((data = br.readLine()) != null) {
//                    Log.i("HexStrConvertUtil", "readInputStream: " + data);
//                    byte[] ba = HexStrConvertUtil.hexStr2Bytes(data);
//                    Log.i("HexStrConvertUtil", "bytes : " + Arrays.toString(ba));
//                }
 
    /**
     * 合并数组
     *
     * @param firstArray  第一个数组
     * @param secondArray 第二个数组
     * @return 合并后的数组
     */
    public static byte[] connect(byte[] firstArray, byte[] secondArray) {
        if (firstArray == null || secondArray == null) {
            return null;
        }
        byte[] bytes = new byte[firstArray.length + secondArray.length];
        System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
        System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
        return bytes;
    }
 
    //合并数组
    public static byte[] connectByte(byte[] firstArray, byte[] secondArray) {
        if (firstArray == null || secondArray == null) {
            return null;
        }
        byte[] bytes = new byte[firstArray.length + secondArray.length];
        System.arraycopy(secondArray,0,bytes,firstArray.length,secondArray.length);
//        System.arraycopy(firstArray,0,secondArray,secondArray.length,bytes.length);
 
//        System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
//        System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
        return bytes;
    }
}