zuoxiao
2024-03-04 275fbc47873d5c4f78a57b4fa58a08e2e7af2dc2
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
package com.dayu.recharge.tools;
 
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
/**
 * Copyright (C), 2022,
 * Author: zuo
 * Date: 2022/3/9 14:55
 * Description: 进制转换
 */
public class HexUtil {
 
 
    /**
     * 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;
    }
 
    public static int getBcdToInt(byte data) {
        return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F));
    }
 
    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }
 
    /**
     * 字节转十六进制
     *
     * @param b 需要进行转换的byte字节
     * @return 转换后的Hex字符串
     */
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex.toUpperCase();
    }
 
 
    /**
     * 字节数组转16进制
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }
 
    public static String byteArrayToHexString(byte[] byteArray) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : byteArray) {
            // 将字节转换为无符号整数
            int unsignedInt = b & 0xff;
            // 将无符号整数转换为16进制字符串
            String hex = Integer.toHexString(unsignedInt);
            // 如果字符串长度小于2,在前面补0
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
 
    /**
     * 字节数组转16进制 不在末尾添加0
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHexNoAddZero(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            sb.append(hex);
        }
        return sb.toString();
    }
 
 
    /**
     * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型
     *
     * @param str 4字节 16进制字符
     * @return
     */
    public static float hexToFloat(String str) {
        return Float.intBitsToFloat(new BigInteger(str, 16).intValue());
    }
 
    /**
     * 将带符号的32位浮点数装换为16进制
     *
     * @param value
     * @return
     */
    public static String folatToHexString(Float value) {
        return Integer.toHexString(Float.floatToIntBits(value));
    }
 
 
    /**
     * 将带符号的32位浮点数装换byte数组
     * 低位在前高位在后
     *
     * @param value
     * @return
     */
    public static byte[] folatToByte(Float value) {
        ByteBuffer buffer = ByteBuffer.allocate(4); // 4个字节
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putFloat(value);
        byte[] byteArray = buffer.array();
        return byteArray;
    }
 
    /**
     * 将byte数组转换为带符号的32位浮点数
     *
     * 低位在前高位在后
     *
     * @param value
     * @return
     */
    public static Float bytesToFloat(byte[] value) {
        ByteBuffer bufferLittleEndian = ByteBuffer.wrap(value);
        bufferLittleEndian.order(ByteOrder.LITTLE_ENDIAN);
        return bufferLittleEndian.getFloat();
 
    }
 
 
    /**
     * 十进制转16进制
     *
     * @param number
     * @return
     */
    public static String get10to16(int number) {
        return Integer.toHexString(number);
    }
 
    /**
     * 十进制转16进制 补齐偶数 高位在前低位在后
     *
     * @param number
     * @return
     */
    public static String get10to16CompleteHex(int number) {
        String hex = Integer.toHexString(number);
        if (hex.length() % 2 == 0) {
            return hex;
        } else {
            return "0" + hex;
        }
    }
 
 
    /**
     * short10进制转16进制 低位在前高位在后
     *
     * @param number
     * @return
     */
    public static String get10To16LowHigh(short number) {
        // 使用 Integer.toHexString 将 short 转换为十六进制字符串
        int intValue = Short.toUnsignedInt(number);
        // 使用 Integer.toHexString 将 int 转换为十六进制字符串
        String hexString = Integer.toHexString(intValue);
        // 补0,确保字符串长度为4
        while (hexString.length() < 4) {
            hexString = "0" + hexString;
        }
 
        hexString = spaceHex(hexString);
        hexString = HighLowHex(hexString);
        return hexString;
    }
 
 
    /**
     * short10进制转16进制 低位在前高位在后
     *
     * @param number
     * @return
     */
    public static String get10To16LowHigh(int number) {
        // 使用 Integer.toHexString 将 int 转换为十六进制字符串
        String hexString = Integer.toHexString(number);
 
        hexString = spaceHex(hexString);
        hexString = HighLowHex(hexString);
        return hexString;
    }
 
 
    /**
     * short16进制转10进制 低位在前高位在后
     *
     * @param hex
     * @return
     */
    public static short getShort10To16LowHigh(String hex) {
        try {
            String str = "";
            str = spaceHex(hex);
            str = HighLowHex(str);
            return (short) Integer.parseInt(str, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }
 
 
    /**
     * 十进制转16进制低位在前高位在后
     *
     * @param number 十进制数
     * @param length 补足多少位
     * @return
     */
    public static String get10to16LowHigh(int number, int length) {
        String str = "";
        try {
            str = Integer.toHexString(number);
            str = getHexToLenght(str, length);
            str = spaceHex(str);
            str = HighLowHex(str);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
 
    /**
     * 16进制转10进制高低位转换
     *
     * @param hex
     * @return
     */
    public static int get16to10LowHigh(String hex) {
        try {
            String str = "";
            str = spaceHex(hex);
            str = HighLowHex(str);
            return Integer.parseInt(str, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }
 
 
    /**
     * 返回特定长度的16进制字符串
     *
     * @param data
     * @param length
     * @return
     */
    public static String getHexToLenght(String data, int length) {
        StringBuffer stringBuilder = new StringBuffer(data);
        for (int i = 0; i < length - data.length(); i++) {
            stringBuilder.insert(0, "0");
        }
        return stringBuilder.toString();
    }
 
    /**
     * 十六进制数隔空位
     *
     * @param str
     * @return
     */
    public static String spaceHex(String str) {
        char[] array = str.toCharArray();
        if (str.length() <= 2) return str;
        StringBuffer bufferHex = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            int start = i + 1;
            if (start % 2 == 0) {
                bufferHex.append(array[i]).append(" ");
            } else {
                bufferHex.append(array[i]);
            }
        }
        return bufferHex.toString();
    }
 
    /**
     * 高位16进制转低位
     *
     * @param str
     * @return
     */
    private static String HighLowHex(String str) {
        if (str.trim().length() <= 2) return str;
        List<String> list = Arrays.asList(str.split(" "));
        Collections.reverse(list);
        StringBuffer stringBuffer = new StringBuffer();
        for (String string : list) {
            stringBuffer.append(string);
        }
        return stringBuffer.toString();
    }
 
    /**
     * @param hex
     * @return
     */
    public static int get16to10(String hex) {
        int x = 0;
        try {
            x = Integer.parseInt(hex, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return x;
    }
}