shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package com.walker.tcp.util;
 
import java.math.BigInteger;
import java.text.DecimalFormat;
 
public class ByteUtils {
 
    /**
     * 2进制
     *
     * @param b
     * @return
     */
    public static String toBinary(byte b) {
        StringBuilder stringBuilder = new StringBuilder();
        String val = ByteUtils.byteToHexString(b);
        BigInteger integer = new BigInteger(val, 16);
        stringBuilder.append(integer.toString(2));
        while (stringBuilder.length() < 8) {
            stringBuilder.insert(0, "0");
        }
        return stringBuilder.toString();
    }
 
    /**
     * 获取高四位
     * @param data
     * @return
     */
    public static int getHeight4(byte data){
        int height;
        height = ((data & 0xf0) >> 4);
        return height;
    }
 
    /**
     * 获取低四位
     * @param data
     * @return
     */
    public static int getLow4(byte data){
        int low;
        low = (data & 0x0f);
        return low;
    }
    /**
     * 去除int最高位
     *
     * @param val
     * @return
     */
    public static int delH(int val) {
        return val = (val << 1 & 0xff) >>> 1;
    }
 
    public static String byteToHexString(byte src) {
        StringBuilder stringBuilder = new StringBuilder("");
        int v = src & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
        return stringBuilder.toString();
    }
 
    /**
     * Convert byte[] to hex string. 把字节数组转化为字符串
     *
     * @param src
     * @param begin
     *            byte[] 起始位置
     * @param len
     *            长度
     * @return
     */
    public static String bytesToHexString(byte[] src, int begin, int len) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        // 这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
        int end = begin + len;
        for (int i = begin; i < end; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
 
    /**
     * Convert byte[] to hex string. 把字节数组转化为字符串
     *
     *
     * @param src
     *            byte[]
     * @return hex string
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        // 这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
 
    /**
     * 空格分隔
     * @param src
     * @return
     */
    public static String bytesToHexString2(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        // 这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv).append(" ");
        }
        return stringBuilder.toString();
    }
 
    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param byteArray
     *            需要转换的字节数组
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");
 
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }
 
 
    /**
     * Convert byte[] to hex string[]. 把字节数组转化为字符串数组
     *
     */
    public static String[] bytesToHexStrings(byte[] src) {
        String s = bytesToHexString(src);
        String[] ss = new String[src.length];
        for (int j = 0; j < ss.length; j++) {
            ss[j] = s.substring(2 * j, 2 * (j + 1));
        }
        return ss;
    }
 
    /**
     * Convert hex string to byte[] 把字符串转化为字节数组
     *
     * @param hexString
     *            the hex string, 两位string一个byte, 允许有空格分隔
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 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;
    }
 
    // ------------
 
    /**
     * 将 10进制int 转换为占一个字节的hexString
     *
     * @param src
     * @return
     */
    public static String int2HexString(int src) {
        StringBuilder stringBuilder = new StringBuilder();
        String hv = Integer.toHexString(src);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
        return stringBuilder.toString();
 
    }
 
    /**
     * 将10进制int转换为占2个字节的hexString(低字节在前)
     *
     * @param src
     * @return
     */
    public static String intToHexString(int src) {
        byte[] b = ByteUtils.toBytes(src);
        return ByteUtils.bytesToHexString(b);
    }
    /**
     * 将10进制int转换为占2个字节的hexString(高字节在前)
     *
     * @param src
     * @return
     */
    public static String intToHexStringH(int src) {
        byte[] b = ByteUtils.toBytesH(src);
        return ByteUtils.bytesToHexString(b);
    }
 
    /**
     * 将10进制String 转换为占2个字节的hexString(低位在前)
     *
     * @param src
     * @return
     */
    public static String toHexString(String src) {
        return intToHexString(Integer.valueOf(src));
    }
 
    /**
     * 将10进制int转换为占4个字节的hexString
     *
     * @param src
     * @return
     */
    public static String intTo4HexString(int src) {
        return ByteUtils.bytesToHexString(ByteUtils.intToBytes(src));
 
    }
 
    /**
     * int -> byte[iArrayLen]
     *
     * @param iSource
     * @param iArrayLen
     *            <=4
     * @return
     */
    public static byte[] toByteArray(int iSource, int iArrayLen) {
        byte[] bLocalArr = new byte[iArrayLen];
        for (int i = 0; i < iArrayLen; i++) {
            // bLocalArr[i] = (byte) (iSource >> 8 * (iArrayLen - i - 1) &
            // 0xFF);
            bLocalArr[i] = (byte) (iSource >> 8 * i);
        }
        return bLocalArr;
    }
 
    /**
     * int -> byte[2]
     *
     * @param
     * @return 低位在前
     */
    public static byte[] toBytes(int v) {
        byte[] targets = new byte[2];
        targets[0] = (byte) (0xff & v);
        targets[1] = (byte) (0xff & (v >> 8));
        return targets;
    }
    /**
     * int -> byte[2]
     *
     * @param
     * @return 高位在前
     */
    public static byte[] toBytesH(int v) {
        byte[] targets = new byte[2];
        targets[1] = (byte) (0xff & v);
        targets[0] = (byte) (0xff & (v >> 8));
        return targets;
    }
 
    /**
     * int -> byte[4]
     * 低位在前
     *
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num) {
        byte[] targets = new byte[4];
 
        targets[0] = (byte) (num & 0xff);// 最低位
        targets[1] = (byte) ((num >> 8) & 0xff);// 次低位
        targets[2] = (byte) ((num >> 16) & 0xff);// 次高位
        targets[3] = (byte) (num >>> 24);// 最高位,无符号右移。
        return targets;
    }
 
    /**
     * 低位在前 ,3位的byte[] 转 int
     *
     * @return
     */
    public static int toInt3(byte[] bs, int begain) {
        int accum = 0;
        accum = bs[begain + 0] & 0xFF;
        accum |= (bs[begain + 1] & 0xFF) << 8;
        accum |= (bs[begain + 2] & 0xFF) << 16;
        return accum;
    }
 
    /**
     * 低位在前 ,4位的byte 转 int
     *
     * @param bs
     *            低位在前 byte数组
     * @return
     */
    public static int toInt4(byte[] bs) {
        int accum = 0;
        accum = bs[0] & 0xFF;
        accum |= (bs[1] & 0xFF) << 8;
        accum |= (bs[2] & 0xFF) << 16;
        accum |= (bs[3] & 0xFF) << 24;
        return accum;
    }
 
    /**
     * 获取 高位在前的4位byte int值<br>
     *
     * @param bs
     *            高位在前 byte数组
     * @return
     */
    public static int toIntByH(byte[] bs, int start) {
        int accum = 0;
        accum   = bs[start + 3] & 0xFF;
        accum |= (bs[start + 2] & 0xFF) << 8;
        accum |= (bs[start + 1] & 0xFF) << 16;
        accum |= (bs[start + 0] & 0xFF) << 24;
        return accum;
    }
 
    /**
     * 将低位在前 ,4位的byte 转 int
     *
     * @param bs
     *            byte数组
     * @param pos
     *            起始位置
     * @return
     */
    public static int toInt4(byte[] bs, int pos) {
        int accum = 0;
        accum = bs[pos + 0] & 0xFF;
        accum |= (bs[pos + 1] & 0xFF) << 8;
        accum |= (bs[pos + 2] & 0xFF) << 16;
        accum |= (bs[pos + 3] & 0xFF) << 24;
        return accum;
    }
 
    /**
     * 低位在前 的 byte[2] 转 int, int*0.01转成double并保留两位小数
     *
     * @param res
     * @param begainIdx
     *            起始位置
     * @return
     */
    public static double toDouble(byte[] res, int begainIdx) {
        Double data = toInt2(res, begainIdx) * 0.01;
        DecimalFormat bd = new DecimalFormat("#.00");
        data = Double.valueOf( bd.format(data) );
 
        return data;
    }
    /**
     * 低位在前 的 byte[2] 转 int
     *
     * @param res
     * @param begainIdx
     *            起始位置
     * @return
     */
    public static int toInt2(byte[] res, int begainIdx) {
        // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
        int targets = (res[begainIdx] & 0xff) | ((res[begainIdx + 1] << 8) & 0xff00);
 
        return targets;
    }
 
    /**
     * byte[] 转 int
     *
     * @param res
     *            低位在前 ,2位的byte数组
     * @return
     */
    public static int toInt2(byte[] res) {
        // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
        return targets;
    }
 
    /**
     * Convert char to byte
     *
     * @param c
     *            char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
 
    /**
     * 获取接收到的信息中的指令码
     *
     * @param msg
     *            接收到的信息
     * @return
     */
    public static String getMsgCmd(byte[] msg) {
        byte[] cmd = { msg[6], msg[7] };
        return bytesToHexString(cmd);
    }
 
    /**
     * 返回命令字符串,如:106
     * @param cmdArea
     * @return
     * @author 时克英
     * @date 2020-08-20
     */
    public static String getCommand(byte[] cmdArea){
        return bytesToHexString(cmdArea);
    }
 
    /**
     * 将i进制字符串转换成j进制字符串
     *
     * @param str
     * @param i
     * @param j
     */
    public static String itoj(String str, int i, int j) {
        String[] strs = str.split("\\s+");
        String result = "";
        for (String string : strs) {
            String hex = Integer.toString(Integer.parseInt(string, i), j);
            result += hex;
        }
        return result;
    }
 
    public static void main(String[] args){
        byte[] b = new byte[] { (byte) 0xAA, (byte) 0xf5 };
        int i = ByteUtils.toInt2(b);
        System.out.println("byte[] value = " + i);
    }
}