YYC
2023-10-24 703c4642be0a57c3b8eaee327d0a95eb0e79de8d
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
package com.ld.igds.util;
 
import java.math.BigInteger;
 
public class BytesUtil {
 
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }
 
    /**
     * bytes输出十进制
     * 
     * @param bytes
     * @return
     */
    public static Integer bytesToInt(byte[] bytes) {
        String str = "";
        int i = 0;
        boolean flag = true && bytes.length > 0;
        while (flag) {
            Byte b = bytes[i];
            i++;
            Integer bi = Byte.toUnsignedInt(b);
            str += toHexString(bi);
            if (i >= bytes.length || str.endsWith("EEEE"))
                flag = false;
        }
        return Integer.parseInt(str, 16);
    }
 
    /**
     * bytes输出十六进制字符串
     * 
     * @param bytes
     * @return
     */
    public static String bytesToString(byte[] bytes) {
        String str = "";
        int i = 0;
        boolean flag = true && bytes.length > 0;
        while (flag) {
            Byte b = bytes[i];
            i++;
            Integer bi = Byte.toUnsignedInt(b);
            str += toHexString(bi);
            if (i >= bytes.length)
                flag = false;
        }
        return str;
    }
 
    /**
     * 十六进制转字节
     * 
     * @param hex
     * @return
     */
    public static byte hexToByte(Integer hex) {
        return hex.byteValue();
    }
 
    /**
     * short转2字节 已调整高低位
     * 
     * @param number
     * @return
     */
    public static byte[] shortToByte(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }
 
    /**
     * 二进制子串转16进制子串
     * 
     * @param bin
     * @return
     */
    public static String binToHex(String bin) {
        String hexStr = "";
        int size = bin.length() / 8;
        for (int i = 0; i < size; i++) {
            String value = bin.substring(i * 8, (i + 1) * 8);
            int temp = Integer.parseInt(value, 2);
            String tempHex = Integer.toHexString(temp);
            hexStr += tempHex;
        }
        return hexStr;
    }
 
    /**
     * 二进制转16进制,不足2位补位
     * 
     * @param bin
     * @return
     */
    public static String bin2Hex(String bin) {
        String hexStr = "";
        int size = bin.length() / 8;
        for (int i = 0; i < size; i++) {
            String value = bin.substring(i * 8, (i + 1) * 8);
            int temp = Integer.parseInt(value, 2);
            String tempHex = Integer.toHexString(temp);
            if (tempHex.length() < 2) {
                tempHex = "0" + tempHex;
            }
            hexStr += tempHex;
        }
 
        return hexStr;
    }
 
    /**
     * 转为十六进制串,不足2位补0
     * 
     * @param value
     * @return
     */
    public static String toHexString(int value) {
        String tempHex = Integer.toHexString(value);
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        return tempHex.toUpperCase();
    }
 
    public static String getTargetId(String value, boolean isTwo) {
        String tempHex = Integer.toHexString(Integer.valueOf(value));
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        if (isTwo) {
            if (tempHex.length() < 4) {
                tempHex = "0" + tempHex;
            }
            if (tempHex.length() < 4) {
                tempHex = "0" + tempHex;
            }
        }
        return tempHex.toUpperCase();
    }
 
    /**
     * 根据数值获取到长度为2字节的16进制字符
     * 
     * @param value
     * @return
     */
    public static String getHex2LenStr(int value) {
        String tempHex = Integer.toHexString(value);
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
        }
        if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
        }
        return tempHex.toUpperCase();
    }
 
    public static String getOrderId(int value) {
        return getHex2LenStr(value);
    }
 
    /**
     * 将value转为same个相同满8位的二进制字串
     * 
     * @param value
     * @param same
     * @return
     */
    public static String toBinary8StringSame(int value, int same) {
        String tempBinStr = toBinary8String(value);
        String rsBinStr = "";
        for (int i = 0; i < same; i++) {
            rsBinStr += tempBinStr;
        }
        return rsBinStr;
    }
 
    public static String hexString2binaryString(String hexString, int num) {
        //16进制转10进制
        BigInteger sint = new BigInteger(hexString, num);
        //10进制转2进制
        String str = sint.toString(2);
        if(str.length() < num){
            for (int i = str.length(); i < num; i++) {
                str = "0" + str;
            }
        }
        return str;
    }
 
    /**
     * 将二进制转换为10进制
     * @param binStr
     * @return
     */
    public  static  Integer biannary2Decimal(String binStr){
 
        Integer sum = 0;
        int len = binStr.length();
 
        for (int i=1;i<=len;i++){
            //第i位 的数字为:
            int dt = Integer.parseInt(binStr.substring(i-1,i));
            sum+=(int)Math.pow(2,len-i)*dt;
        }
        return  sum;
    }
 
    public static void main(String[] args) {
        String s = hexString2binaryString("21E9", 16);
        System.out.println(s);
    }
 
    /**
     * 不足width个字节宽度时,前面补0至width*8
     * 
     * @param value
     * @param width
     * @return
     */
    public static String toBinary8String(int value, int width) {
        String tempBinStr = toBinary8String(value);
        int size = tempBinStr.length();
        for (int i = 0; i < width; i++) {
            int temp = (i + 1) * 8;
            if (temp > size) {
                tempBinStr = "00000000" + tempBinStr;
                size = tempBinStr.length();
            }
        }
        return tempBinStr;
    }
 
    /**
     * 补足8位
     * 
     * @param value
     * @return
     */
    public static String toBinary8String(int value) {
        String temp = Integer.toBinaryString(value);
        if (value == 0) {
            temp = "00000000";
        }
        int length = temp.length();
        while (length < 8) {
            temp = "0" + temp;
            length = temp.length();
        }
        return temp;
    }
 
    public static String toEmptyBinaryWidthString(int width) {
        String str = "";
        for (int i = 0; i < width; i++) {
            str += "00000000";
        }
        return str;
    }
 
    public static byte[] emptyBytes(int bytes) {
        byte[] bys = new byte[bytes];
        for (int i = 0; i < bytes; i++) {
            bys[i] = emptyByte();
        }
        return bys;
    }
 
    public static byte emptyByte() {
        Integer b0 = 0x00;
        return b0.byteValue();
    }
 
    public static byte binToBytes(String bin) {
        return Integer.valueOf(bin, 2).byteValue();
    }
 
    public static byte[] binaryStrToBytes(String binStr) {
        int size = binStr.length() / 8;
        byte[] bs = new byte[size];
        for (int i = 0; i < size; i++) {
            String temp = binStr.substring(i * 8, (i + 1) * 8);
            bs[i] = Integer.valueOf(temp, 2).byteValue();
        }
        return bs;
    }
 
    /**
     * 十六进制串转字节数组
     * 
     * @param hexStr
     * @return
     */
    public static byte[] hexStrToBytes(String hexStr) {
        int size = hexStr.length() / 2;
        byte[] bytes = new byte[size];
        for (int i = 0; i < size; i++) {
            String tmp = hexStr.substring(i * 2, (i + 1) * 2);
            Integer tmpHex = Integer.parseInt(tmp, 16);
            bytes[i] = tmpHex.byteValue();
        }
        return bytes;
    }
 
    /**
     * 两字节高低位装换
     *
     * @param b
     * @return
     */
    public static byte[] hexHeightLow(byte[] b) {
        byte[] b2 = new byte[2];
        // 转换高低位
        b2[0] = b[1];
        b2[1] = b[0];
        return b2;
    }
 
    /**
     * 16进制转换为10进制
     * 
     * @param strHex
     * @return
     */
    public static int hexToInt(String strHex) {
        short s = (short) (Integer.valueOf(strHex, 16) & 0xffff);
        return s;
    }
 
    /**
     * 16进制转换为10进制
     * 
     * @param strHex
     * @return
     */
    public static int hexToBigInt(String strHex) {
        BigInteger bigint = new BigInteger(strHex, 16);
        return bigint.intValue();
    }
 
    /**
     * int转16进制字符串 2位 00 00
     *
     * @param num
     * @return
     */
    public static String intToHexStr(int num) {
        // 需要使用2字节表示b
        return String.format("%04x", num).toUpperCase();
    }
 
    /**
     * int转16进制字符串 1位置 00
     *
     * @param num
     * @return
     */
    public static String intToHexStr1(int num) {
        // 需要使用2字节表示b
        return String.format("%02x", num).toUpperCase();
    }
 
 
    // 计算16进制对应的数值
    public static int GetHex(char ch) throws Exception {
        if (ch >= '0' && ch <= '9')
            return (int) (ch - '0');
        if (ch >= 'a' && ch <= 'f')
            return (int) (ch - 'a' + 10);
        if (ch >= 'A' && ch <= 'F')
            return (int) (ch - 'A' + 10);
        throw new Exception("error param");
    }
 
    // 计算幂
    public static int GetPower(int nValue, int nCount) throws Exception {
        if (nCount < 0)
            throw new Exception("nCount can't small than 1!");
        if (nCount == 0)
            return 1;
        int nSum = 1;
        for (int i = 0; i < nCount; ++i) {
            nSum = nSum * nValue;
        }
        return nSum;
    }
 
    /**
     * 16进制字符串 高低换位
     *
     * @param info
     * @return
     */
    public static String tran_LH(String info) {
        return info.substring(2) + info.substring(0, 2);
    }
 
    /**
     * 16进制字符串 高低换位 8个字符
     *
     * @param info
     * @return
     */
    public static String tran_LH8(String info) {
        return tran_LH(info.substring(4)) + tran_LH(info.substring(0, 4));
    }
 
    /**
     * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型
     * 
     * 42c60000 -> 99.00
     * 
     * @param str
     *            4字节 16进制字符
     * @return
     */
    public static float hexToFloat(String str) {
        return Float.intBitsToFloat(new BigInteger(str, 16).intValue());
    }
 
    /**
     * 将带符号的32位浮点数装换为16进制
     * 
     * 99.00 -> 42c60000
     * 
     * 
     * @param value
     * @return
     */
    public static String folatToHexString(Float value) {
        return Integer.toHexString(Float.floatToIntBits(value));
    }
}