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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package tbDataModel_Dell;
import java.math.BigInteger;
public class Jiaoyan {
    
    /**»ñȡУÑéÂë³ýÈ¥°üÍ·Ïà¼ÓÈ¡·´*/
    public static byte[] check(byte[] byt) {
        
        //×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®/2ºóµÄ³¤¶È
        int size=BinaryToHexString2(byt).length()/2;
        String[] hex=hex(BinaryToHexString2(byt));
        
        //ÇóºÍ
        int sum=0;
        for(int i=0;i<size;i++) {
            sum+=decodeHEX(hex[i]);
        }
        
        //È¡·´×ªÎª16½øÖÆ×Ö·û´®
        String hex16=Integer.toHexString(~sum);
        
        //×Ö·û´®µÄ³¤¶È
        int lenth=hex16.length();        
        
        byte[] jiaoyan=hexStringToByteArray(hex16.substring(lenth-4, lenth));
        
        return jiaoyan;        
        
    }
    
    /**ÇóºÍУÑé*/
    public static byte[] checkqiuhe(byte[] byt) {
        
        //×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®/2ºóµÄ³¤¶È
        int size=BinaryToHexString2(byt).length()/2;
        String[] hex=hex(BinaryToHexString2(byt));
        
        //ÇóºÍ
        int sum=0;
        for(int i=0;i<size;i++) {
            sum+=decodeHEX(hex[i]);
        }
        
        //È¡·´×ªÎª16½øÖÆ×Ö·û´®
        String hex16=Integer.toHexString(sum);
        
        //×Ö·û´®µÄ³¤¶È
        int lenth=hex16.length();        
        
        byte[] jiaoyan=hexStringToByteArray(hex16.substring(lenth-2, lenth));
        
        return jiaoyan;        
        
    }
    
    
    /**16½øÖÆ×ªÎª10½øÖÆ*/
    public static int decodeHEX(String hexs){
        BigInteger bigint=new BigInteger(hexs, 16);
        int numb=bigint.intValue();
        return numb;
 
    }
    
    /**½«16½øÖÆ×Ö·û´®×ªÎªhex×Ö·û´®Êý×é2¸ö×Ö·û´®Ò»¸ö*/
    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½øÖÆ×Ö·û´®ÎÞ¿Õ¸ñ*/
    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;
    }
    
    
    /**
     * 16½øÖƱíʾµÄ×Ö·û´®×ª»»Îª×Ö½ÚÊý×é
    *
    * @param hexString 16½øÖƱíʾµÄ×Ö·û´®
    * @return byte[] ×Ö½ÚÊý×é
    */
   public static byte[] hexStringToByteArray(String hexString) {
       hexString = hexString.replaceAll(" ", "");
       int len = hexString.length();
       byte[] bytes = new byte[len / 2];
       for (int i = 0; i < len; i += 2) {
           // Á½Î»Ò»×飬±íʾһ¸ö×Ö½Ú,°ÑÕâÑù±íʾµÄ16½øÖÆ×Ö·û´®£¬»¹Ô­³ÉÒ»¸ö×Ö½Ú
           bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                   .digit(hexString.charAt(i+1), 16));
       }
       return bytes;
   }
 
}