左晓为主开发手持机充值管理机
zuoxiao
2024-06-13 d1573513f0eada6a915c2163ce6c84451c8c1070
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
package com.dayu.baselibrary.tools;
 
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
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 {
 
    /**
     * 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;
    }
 
 
    /**
     * 16进制转10进制并且低位在前高位在后
     *
     * @param data
     * @return
     */
    public static int get16To10LowHightByBytes(byte[] data) {
        List<Byte> byteList = new ArrayList<>();
        for (byte b : data) {
            byteList.add(b);
        }
        Collections.reverse(byteList);
        byte[] byteArray = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i);
        }
        String hex = bytesToHex(byteArray);
        return Integer.parseInt(hex, 16);
    }
 
 
 
    public static int getBcdToInt(byte data) {
        return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F));
    }
 
 
    public static byte[] getIntToBCD(int number) {
        // 获取整数的字符串表示形式
        String numberStr = Integer.toString(number);
 
        // 创建一个字节数组以存储BCD表示
        int len = numberStr.length();
        byte[] bcd = new byte[(len + 1) / 2];
 
        int j = 0;
        // 如果数字的长度是奇数,需要在前面添加一个0
        if (len % 2 != 0) {
            numberStr = "0" + numberStr; // 先添加前导零
            len++;
        }
 
        // 将每一对数字转换为一个字节
        for (int i = 0; i < len; i += 2) {
            bcd[j++] = (byte) (((numberStr.charAt(i) - '0') << 4) | (numberStr.charAt(i + 1) - '0'));
        }
 
        return bcd;
    }
    /**
     * 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;
    }
 
    /**
     * 将byte数组转换为带符号的32位浮点数
     * <p>
     * 低位在前高位在后
     *
     * @param value
     * @return
     */
    public static Float bytesToFloat(byte[] value) {
        ByteBuffer bufferLittleEndian = ByteBuffer.wrap(value);
        bufferLittleEndian.order(ByteOrder.LITTLE_ENDIAN);
        return bufferLittleEndian.getFloat();
    }
 
 
    /**
     * 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);
    }
 
    /**
     * 字节转十六进制
     *
     * @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();
    }
 
 
    /**
     * 将 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));
    }
 
 
    /**
     * float转Hex低位在前高位在后
     *
     * @param value
     * @return
     */
    public static String floatToHexLowHigh(Float value) {
        String hexString = Integer.toHexString(Float.floatToIntBits(value));
        hexString = spaceHex(hexString);
        hexString = HighLowHex(hexString);
        return hexString;
    }
 
 
    public static float hexToFloatLowHigh(byte[] data) {
        List<Byte> byteList = new ArrayList<>();
        for (byte b : data) {
            byteList.add(b);
        }
        Collections.reverse(byteList);
        byte[] byteArray = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i);
        }
        String hex = bytesToHex(byteArray);
        int intValue = Integer.parseInt(hex, 16); // 将十六进制字符串转换为整数表示
        float floatValue = Float.intBitsToFloat(intValue); // 将整数表示转换为浮点数
        return floatValue;
    }
 
    /**
     * 十进制转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;
        }
    }
 
    /**
     * 十进制转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进制字符串
     *
     * @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) {
        if (str.length() % 2 != 0) {
            //奇数时前面加0补齐称为偶数
            str = "0" + 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
     */
    public 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;
    }
}