zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
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
package Method;
/**Ö÷Òª°üº¬Ê®Áù½øÖÆ×Ö·û´®×ªASCII£¬ASCIIתʮÁù½øÖÆ×Ö·û´®ÒÔ¼°Ê®Áù½øÖÆ×Ö·û´®×ªByteÊý×éµÈ·½·¨*/
public class HexConvert {
 
    public static String  convertStringToHex(String str){
 
        char[] chars = str.toCharArray();
 
        StringBuffer hex = new StringBuffer();
        for(int i = 0; i < chars.length; i++){
            hex.append(Integer.toHexString((int)chars[i]));
        }
 
        return hex.toString();
    }
 
    public static String convertHexToString(String hex){
 
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
 
        for( int i=0; i<hex.length()-1; i+=2 ){
 
            String s = hex.substring(i, (i + 2));
            int decimal = Integer.parseInt(s, 16);
            sb.append((char)decimal);
            sb2.append(decimal);
        }
 
        return sb.toString();
    }
    
    /**½«16½øÖÆ×Ö·û´®×ªÎªbyteÊý×é*/
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        // toUpperCase½«×Ö·û´®ÖеÄËùÓÐ×Ö·ûת»»Îª´óд
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        // toCharArray½«´Ë×Ö·û´®×ª»»ÎªÒ»¸öеÄ×Ö·ûÊý×é¡£
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    //·µ»ØÆ¥Åä×Ö·û
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
 
    //½«×Ö½ÚÊý×éת»»ÎªshortÀàÐÍ£¬¼´Í³¼Æ×Ö·û´®³¤¶È
    public static short bytes2Short2(byte[] b) {
        short i = (short) (((b[1] & 0xff) << 8) | b[0] & 0xff);
        return i;
    }
    
    /**½«×Ö½ÚÊý×éת»»Îª16½øÖÆ×Ö·û´®*/
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }
    
    /**½«×Ö½ÚÊý×éת»»Îª16½øÖÆ×Ö·û´®ÎÞ¿Õ¸ñ*/
    public static String BinaryToHexString2(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + "";
        }
        return result;
    }
 
 
}