wjc
2025-04-09 87cd5df70918e6ba1af849c5f026d3719bfdb1ac
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
package com.hdl.sdk.link.core.utils;
 
import android.util.Log;
 
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
 
/**
 * Created by Zoro
 * Created on 2021/9/17
 * description:
 */
public class HexUtil {
    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }
 
    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }
 
    protected static char[] encodeHex(byte[] data, char[] toDigits) {
        if (data == null)
            return null;
        int l = data.length;
        char[] out = new char[l << 1];
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0x0F & data[i]];
        }
        return out;
    }
 
 
    public static String encodeHexStr(byte[] data) {
        return encodeHexStr(data, true);
    }
 
    public static String encodeHexStr(byte[] data, boolean toLowerCase) {
        return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }
 
 
    protected static String encodeHexStr(byte[] data, char[] toDigits) {
        return new String(encodeHex(data, toDigits));
    }
 
    public static String formatHexString(byte[] data) {
        return formatHexString(data, false);
    }
 
    public static Integer  createRandom(){
        Random random = new Random();
 
        // 生成一个随机整数
        int randomInt = random.nextInt(65535);
        return randomInt;
    }
 
    public static String formatHexString(byte[] data, boolean addSpace) {
        if (data == null || data.length < 1)
            return null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            String hex = Integer.toHexString(data[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex);
            if (addSpace)
                sb.append(" ");
        }
        return sb.toString().trim();
    }
 
    public static byte[] decodeHex(char[] data) {
 
        int len = data.length;
 
        if ((len & 0x01) != 0) {
            throw new RuntimeException("Odd number of characters.");
        }
 
        byte[] out = new byte[len >> 1];
 
        // two characters form the hex value.
        for (int i = 0, j = 0; j < len; i++) {
            int f = toDigit(data[j], j) << 4;
            j++;
            f = f | toDigit(data[j], j);
            j++;
            out[i] = (byte) (f & 0xFF);
        }
 
        return out;
    }
 
    /**
     * 将十六进制字符串解码为byte[]
     *
     * @param hexStr 十六进制String
     * @return byte[]
     */
    public static byte[] decodeHex(String hexStr) {
        if ("".equals(hexStr)) {
            return null;
        }
        hexStr = hexStr.replace(" ","");
        return decodeHex(hexStr.toCharArray());
    }
 
    public static byte[] addAll(byte[]... arrays) {
        if (arrays.length == 1) {
            return arrays[0];
        }
 
        // 计算总长度
        int length = 0;
        for (byte[] array : arrays) {
            if (null != array) {
                length += array.length;
            }
        }
 
        final byte[] result = new byte[length];
        length = 0;
        for (byte[] array : arrays) {
            if (null != array) {
                System.arraycopy(array, 0, result, length, array.length);
                length += array.length;
            }
        }
        return result;
    }
 
 
 
    protected static int toDigit(char ch, int index) {
        int digit = Character.digit(ch, 16);
        if (digit == -1) {
            throw new RuntimeException("Illegal hexadecimal character " + ch
                    + " at index " + index);
        }
        return digit;
    }
 
 
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        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;
    }
 
    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
 
    public static String extractData(byte[] data, int position) {
        return HexUtil.formatHexString(new byte[]{data[position]});
    }
 
    public static String bytesToHexString(byte[] data) {
        String result = "";
        for (int i = 0; i < data.length; i++) {
            String hexString = Integer.toHexString(data[i] & 0xFF);
            if (hexString.length() == 1) {
                hexString = '0' + hexString;
            }
            result += hexString.toUpperCase();
        }
        return result;
    }
 
    /**
     * byte转16进制
     *
     * @param b
     * @return
     */
    public static String binaryToHex(byte b) {
        String str = byteToBit(b);
        String hexStr = Integer.toHexString(Integer.parseInt(str, 2));
        StringBuffer stringBuffer = new StringBuffer();
        if (hexStr.length() == 1) {
            stringBuffer.append("0");
        }
        stringBuffer.append(hexStr);
        return stringBuffer.toString().toUpperCase();
    }
 
    /**
     * byte转十进制
     *
     * @param b
     * @return
     */
    public static int binaryToDecimal(byte b) {
        String str = byteToBit(b);
        return Integer.parseInt(str, 2);
    }
 
    /**
     * Byte转Bit
     */
    public static String byteToBit(byte b) {
        return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
    }
 
    /**
     * int转byte
     *
     * @param res
     * @return
     */
    public static byte[] int2byte(int res) {
        byte[] targets = new byte[2];
 
        targets[1] = (byte) (res & 0xff);// 最低位
        targets[0] = (byte) ((res >> 8) & 0xff);// 次低位
        /*targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移*/
        return targets;
    }
 
    /**
     * Bit转Byte
     */
    public static byte bitToByte(String byteStr) {
        int re, len;
        if (null == byteStr) {
            return 0;
        }
        len = byteStr.length();
        if (len != 4 && len != 8) {
            return 0;
        }
        if (len == 8) {// 8 bit处理
            if (byteStr.charAt(0) == '0') {// 正数
                re = Integer.parseInt(byteStr, 2);
            } else {// 负数
                re = Integer.parseInt(byteStr, 2) - 256;
            }
        } else {// 4 bit处理
            re = Integer.parseInt(byteStr, 2);
        }
        return (byte) re;
    }
 
    /**
     * 解析体脂数据
     *
     * @param first
     * @param second
     * @param b
     * @return
     */
    public static double getData(int first, int second, byte[] b) {
        double data = ((b[first] & 0xFF) << 8) + (b[second] & 0xFF);
        Log.e("", "data = " + data);
        return data / 10;
    }
 
    /**
     * 解析体脂数据
     *
     * @param first
     * @param second
     * @param b
     * @return
     */
    public static int getDataInt(int first, int second, byte[] b) {
        int data = ((b[first] & 0xFF) << 8) + (b[second] & 0xFF);
        Log.e("", "getDataInt = " + data);
        return data;
    }
 
 
    // 获取系统时间
    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        return str;
    }
 
    // 获取系统日期
    public static String getDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        return str;
    }
 
    /**
     * 获取当前系统时间
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        return str;
    }
 
    /**
     * 合并数组
     *
     * @return
     */
    public static byte[] concat(byte[] b, byte[] mByte) {
        byte[] result = Arrays.copyOf(b, b.length + mByte.length);
        System.arraycopy(mByte, 0, result, b.length, mByte.length);
        return result;
    }
 
    /**
     * 字符串长度等于1的话,补0
     *
     * @param str
     * @return
     */
    public static String addZero(String str) {
        //L.i(ParseData.class, "addZero");
        StringBuffer sBuffer = new StringBuffer();
        if (str.length() == 1) {
            sBuffer.append("0");
            sBuffer.append(str);
            return sBuffer.toString();
        } else {
            return str;
        }
    }
 
    public static double getPercent(double d) {
        if (d > 100) {
            return 0.0;
        }
        return d;
    }
 
    public static String byte2String(byte[] bytes) {
        int pos = bytes.length - 1;
        for (int i = 0; i < bytes.length; i++) {
 
            if (i != (bytes.length - 1) && (bytes[i] & 0xFF) == 0) {
                pos = i - 1;
                break;
            }
        }
        byte[] remarkBytes = new byte[pos + 1];
        System.arraycopy(bytes, 0, remarkBytes, 0, remarkBytes.length);
 
        String result = "";
 
        try {
            result = new String(remarkBytes, "GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
 
    /**
     * byte数组转str
     *
     * @param b
     * @return
     */
    public static String byteArr2Str(byte[] b) {
        if (b.length != 0) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("[");
            for (int i = 0; i < b.length; i++) {
                stringBuffer.append(addZero(binaryToHex(b[i])));
                if (i < b.length - 1) {
                    stringBuffer.append(",");
                }
            }
            stringBuffer.append("]");
            return stringBuffer.toString();
        }
        return null;
    }
 
    /**
     * 保留小数点后count位
     *
     * @param d
     * @param count
     * @return
     */
    public static double keepDecimal(double d, int count) {
        BigDecimal decimal = new BigDecimal(d);
        return decimal.setScale(count, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    /**
     * kg转lb
     *
     * @param weight
     * @return
     */
    public static double kg2lb(double weight) {
        return keepDecimal(weight * 2.2046226, 1);
    }
 
    /**
     * kg转jin
     *
     * @param weight
     * @return
     */
    public static double kg2jin(double weight) {
        return keepDecimal(weight * 2, 1);
    }
 
    /**
     * kg转st
     *
     * @param weight
     * @return
     */
    public static String kg2st(double weight) {
        double lbData = kg2lb(weight);
        StringBuffer stringBuffer = new StringBuffer();
        int lb = (int) (lbData / 14);
        int st = (int) lbData % 14;
        stringBuffer.append(lb);
        stringBuffer.append(":");
        stringBuffer.append(addZero(String.valueOf(st)));
        return stringBuffer.toString();
    }
 
    public static String int2HexStr(int i) {
        return binaryToHex(Integer.valueOf(i).byteValue());
    }
 
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] chars = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(chars[pos]) << 4 | toByte(chars[pos + 1]));
        }
        return result;
    }
 
    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
 
    public static String formatTo1(double f) {
        BigDecimal bg = new BigDecimal(f);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
    }