左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
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
package com.dayu.recharge.tools;
 
import java.math.BigInteger;
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;
    }
 
 
    /**
     * 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;
    }
 
 
    /**
     * 字节数组转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));
    }
 
    /**
     * 十进制转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) {
        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;
    }
}