zsh_root
2025-01-06 7857a444de69124e9f7fb45f98b0ae818b107f23
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
package tools;
 
import java.math.BigInteger;
 
@SuppressWarnings("unused")
public class Tools {
        
    
    
    /**
     * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×é
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr(String hexString) {
        
 
        if (hexString==null) {
            return null;
        }
 
        hexString = hexString.replaceAll(" ", "");
 
        int len = hexString.length();
        int index = 0;
 
 
        byte[] bytes = new byte[len / 2];
 
        while (index < len) {
 
            String sub = hexString.substring(index, index + 2);
            bytes[index/2] = (byte)Integer.parseInt(sub,16);
            index += 2;
        }
 
 
        return bytes;
    }
    
    /**
     * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×鲻ȥ³ý¿Õ¸ñ
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToBinaryStr2(String hexString) {
         
        if (hexString==null) {
            return null;
        } 
        
        int len = hexString.length();
        
        int index = 0;
 
 
        byte[] bytes = new byte[len / 2];
 
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index/2] = (byte)Integer.parseInt(sub,16);
            index += 2;
        }
 
 
        return bytes;
    }
    
    
    
     public static final int registersToInt(byte[] bytes) {
            return (
                ((bytes[0] & 0xff) << 24) |
                ((bytes[1] & 0xff) << 16) |
                ((bytes[2] & 0xff) << 8) |
                (bytes[3] & 0xff)
                );
          }//registersToInt
 
 
    // hexתASCII
    public static String hexToAscii(String hex) {
        StringBuilder ascii = new StringBuilder();
        // È¥³ý HEX ×Ö·û´®ÖеĿոñ
        hex = hex.replaceAll(" ", "");
 
        // Ã¿Á½¸ö×Ö·ûΪһ×飬ת»»Îª¶ÔÓ¦µÄ×Ö½Ú²¢Æ´½Ó³É ASCII ×Ö·û
        for (int i = 0; i < hex.length(); i += 2) {
            // Ö±½ÓʹÓàcharAt »ñȡÿÁ½¸ö×Ö·ûµÄÊ®Áù½øÖÆÊý×Ö
            char hexChar1 = hex.charAt(i);
            char hexChar2 = hex.charAt(i + 1);
 
            // ×éºÏÊ®Áù½øÖÆ×Ö·ûΪһ¸öÊý×Ö
            int decimalValue = (Character.digit(hexChar1, 16) << 4) + Character.digit(hexChar2, 16);
 
            // ×ª»»Îª¶ÔÓ¦µÄ ASCII ×Ö·û²¢Ìí¼Óµ½½á¹ûÖÐ
            ascii.append((char) decimalValue);
        }
 
        return ascii.toString();  // ·µ»ØÆ´½ÓºóµÄ ASCII ×Ö·û´®
    }
 
 
 
    public static String byteToAscii(byte[] bytes) {
        // ½«×Ö½ÚÊý×éת»»Îª ASCII ×Ö·û´®
        String asciiString = new String(bytes, java.nio.charset.StandardCharsets.US_ASCII);
        return asciiString;
    }
 
    /**½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®*/ 
     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 += hex.toUpperCase(); 
               }
        return ret;
       }
 
 
 
 
 
 
 
     public static final float registersToFloat(byte[] bytes) {
            return Float.intBitsToFloat((
                ((bytes[0] & 0xff) << 24) |
                ((bytes[1] & 0xff) << 16) |
                ((bytes[2] & 0xff) << 8) |
                (bytes[3] & 0xff)
                ));
          }//
     @SuppressWarnings("unused")
    private static String intToHex(int n) {
            StringBuffer s = new StringBuffer();
            String a;
            char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
            while(n != 0){
                s = s.append(b[n%16]);
                n = n/16;            
            }
            a = s.reverse().toString();
            return a;
        }
     
     //½«·µ»ØµÄÊý¾Ýת»¯Îª10½øÖÆÊý¾Ý
     public static int ormntoint(byte[] bytes){
         int aa=0;
         aa=bytes[0]-48 +(bytes[1]-48)*10+(bytes[2]-48)*100+(bytes[3]-48)*1000;
         return aa;
     }
 
 
    /**
     * 16½øÖƵÄ×Ö·û´®±íʾת³É×Ö½ÚÊý×é
     *
     * @param hexString 16½øÖƸñʽµÄ×Ö·û´®
     * @return ×ª»»ºóµÄ×Ö½ÚÊý×é
     **/
    public static byte[] toByteArray(String hexString) {
        byte[] byteArray=null;
        if(hexString !=null || hexString !="") {
            hexString = hexString.replaceAll(" ", "");
            byteArray = new byte[hexString.length() / 2];
            int k = 0;
            for (int i = 0; i < byteArray.length; i++) {//ÒòΪÊÇ16½øÖÆ£¬×î¶àÖ»»áÕ¼ÓÃ4λ£¬×ª»»³É×Ö½ÚÐèÒªÁ½¸ö16½øÖƵÄ×Ö·û£¬¸ßλÔÚÏÈ
                byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
                byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
                byteArray[i] = (byte) (high << 4 | low);
                k += 2;
            }
        }
        return byteArray;
    }
 
 
    public static  byte[] intToRegisters(int v) {
        byte[] registers = new byte[4];
        registers[0] = (byte) (0xff & (v >> 24));
        registers[1] = (byte) (0xff & (v >> 16));
        registers[2] = (byte) (0xff & (v >> 8));
        registers[3] = (byte) (0xff & v);
        return registers;
    }
 
 
 
    //ת³É×Ö½ÚÊý×é
    public static String[] hex(String message) {
        int size=message.length()/2;
        String[] hex=new String[size];
        for(int i=0;i<size;i++) {
            hex[i]=message.substring(i*2, 2+i*2);
        }
 
        return hex;
    }
 
 
    /**
     * 16½øÖÆ×ªÎª10½øÖÆ
     */
    public static int decodeHEX(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        int numb = bigint.intValue();
        return numb;
    }
 
    public static byte decodeHEXToInt8(String hexStr) {
        BigInteger bigInt = new BigInteger(hexStr, 16);
        // ½«ÎÞ·ûºÅÕûÊýת»»ÎªÓзûºÅµÄ8λÕûÊý
        byte result = bigInt.byteValue();
        return result;
    }
}