kongdeqiang
2023-06-08 3fba1d84220268d871c3c28e0e25f6eab3526f46
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
package com.boying.util;
 
import java.util.Arrays;
import java.util.UUID;
 
public class StringUtil {
 
    /** 16进制中的字符集 */
    private static final String HEX_CHAR = "0123456789ABCDEF";
 
    /** 16进制中的字符集对应的字节数组 */
    private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes();
 
    public static boolean isNullOrEmpty(String str) {
        if (null == str || "".equalsIgnoreCase(str.trim())
                || "null".equals(str.trim())) {
            return true;
        }
        return false;
    }
    public static byte[] convertNum(String s){
        String[] split = s.split("-");
        byte[] bytes = new byte[split.length];
        for (int i = 0; i < split.length; i++) {
            String s1 = split[i];
            byte b = Byte.parseByte("0x"+s1);
            bytes[i]=b;
        }
        return bytes;
    }
 
    public static String toStr(String[] strs){
        String s = Arrays.toString(strs);
        String substring = s.substring(1, s.length() - 1);
        String s1 = substring.replaceAll(",", "");
        String s2 = s1.replaceAll(" ", "");
        return s2;
    }
 
    public static byte[] toBytes(String strs){
        String[] split = strs.split("-");
        byte[] bytes = new byte[split.length];
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            bytes[i] = (byte) Byte.parseByte(s);
        }
        return bytes;
    }
 
    public static byte[] hex2Byte(String hex) {
        System.out.println();
        String[] parts = hex.split("-");
        byte[] bytes = new byte[parts.length];
        for (int i = 0; i < parts.length; i++) {
            bytes[i] = (byte) (Integer.parseInt(parts[i],10));
            System.out.print(bytes[i]+ " ");
        }
        return bytes;
    }
 
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
    /**
     * 将指定byte数组以16进制的形式打印到控制台
     *
     * @param hint String
     * @param b    byte[]
     * @return void
     */
    public static void printHexString(String hint, byte[] b) {
        System.out.print(hint);
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            System.out.print(hex.toUpperCase() + " ");
        }
    }
 
    /**
     * @param b byte[]
     * @return String
     */
    public static String Bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += " 0x" + hex.toUpperCase();
        }
        return ret;
    }
 
    /**
     * 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF,
     * 0xD9}
     *
     * @param src String
     * @return byte[]
     */
    public static byte[] HexString2Bytes(String src) {
        if (null == src || 0 == src.length()) {
            return null;
        }
       // String s1 = src.replaceAll("-", "");
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < (tmp.length / 2); i++) {
            ret[i] = (byte)uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }
 
    /**
     * 将两个ASCII字符合成一个字节; 如:"EF"–> 0xEF
     *
     * @param src0 byte
     * @param src1 byte
     * @return byte
     */
    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }
 
    /**
     * 10进制字节数组转换为16进制字节数组
     *
     * byte用二进制表示占用8位,16进制的每个字符需要用4位二进制位来表示,则可以把每个byte
     * 转换成两个相应的16进制字符,即把byte的高4位和低4位分别转换成相应的16进制字符,再取对应16进制字符的字节
     *
     * @param b 10进制字节数组
     * @return 16进制字节数组
     */
    public static byte[] byte2hex(byte[] b) {
        int length = b.length;
        byte[] b2 = new byte[length << 1];
        int pos;
        for(int i=0; i<length; i++) {
            pos = 2*i;
            b2[pos] = HEX_STRING_BYTE[(b[i] & 0xf0) >> 4];
            b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f];
        }
        return b2;
    }
 
    /**
     * 16进制字节数组转换为10进制字节数组
     *
     * 两个16进制字节对应一个10进制字节,则将第一个16进制字节对应成16进制字符表中的位置(0~15)并向左移动4位,
     * 再与第二个16进制字节对应成16进制字符表中的位置(0~15)进行或运算,则得到对应的10进制字节
     * @param b 10进制字节数组
     * @return 16进制字节数组
     */
    public static byte[] hex2byte(byte[] b) {
        if(b.length%2 != 0) {
            throw new IllegalArgumentException("byte array length is not even!");
        }
 
        int length = b.length >> 1;
        byte[] b2 = new byte[length];
        int pos;
        for(int i=0; i<length; i++) {
            pos = i << 1;
            b2[i] = (byte) (HEX_CHAR.indexOf( b[pos] ) << 4 | HEX_CHAR.indexOf( b[pos+1] ) );
        }
        return b2;
    }
 
    /**
     * 将16进制字节数组转成10进制字符串
     * @param b 16进制字节数组
     * @return 10进制字符串
     */
    public static String hex2Str(byte[] b) {
        return new String(hex2byte(b));
    }
 
    /**
     * 将10进制字节数组转成16进制字符串
     * @param b 10进制字节数组
     * @return 16进制字符串
     */
    public static String byte2HexStr(byte[] b) {
        return Integer.toHexString(Integer.parseInt(new String(b)));
    }
 
 
    public static void main(String[] args) {
 
    }
}