lizhanwei
2020-02-19 4d207cda87d3685a7edfbb4c62887807d2c6ccf1
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
package com.safeluck.aykj.utils;
 
public final class BytesUtils {
    public static String bytesToHexString(byte[] src,int len){  
        return bytesToHexString(src,0,len);
    }
    public static String bytesToHexString(byte[] src){  
        return bytesToHexString(src,0,src.length);
    }
 
    public static String bytesToHexString(byte[] src,int start,int len){  
        StringBuilder stringBuilder = new StringBuilder("");  
        if (src == null || src.length <= 0) {  
            return null;  
        }  
        for (int i = start; i < len; i++) {  
            int v = src[i] & 0xFF;  
            String hv = Integer.toHexString(v);  
            if (hv.length() < 2) {  
                stringBuilder.append(0);  
            }  
            stringBuilder.append(hv);   
        }  
        return stringBuilder.toString().toUpperCase();  
    }  
    public static byte[] hexStringToBytes(String hexString) {  
        if (hexString == null || hexString.equals("")) {  
            return null;  
        }  
        hexString = hexString.toUpperCase();  
        int length = hexString.length() / 2;  
        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);  
    }
 
    public static byte xor(byte[] bytes,int start,int end)
    {
        if (bytes.length < end)
        {
            throw new RuntimeException("计算xor的时候size不足");
        }
        int s1 = bytes[start];
        for (int i = start + 1; i < end; i++)
        {
            s1 = s1 ^ bytes[i];
        }
        return (byte)s1;
    }
 
 
    public static byte xor(byte[] bytes)
    {
        return xor(bytes,0,bytes.length);
    }
    public static String toHexString(byte b)
    {
        String hex = Integer.toHexString(b&0xff).toUpperCase();
        if(hex.length()==1)
        {
            return "0"+hex;
        }
        return hex;
    }
    public static int sum(byte[] bytes,int start,int end)
    {
        if (bytes.length < end)
        {
            throw new RuntimeException("计算sum的时候size不足");
        }
        int s1 = 0;
        for (int i = start; i < end; i++)
        {
//            System.out.println(bytes[i]&0xFF);
            s1 += (bytes[i]&0xFF);
//            System.out.println("sum="+s1);
        }
        return s1;
 
 
//        int sum = 0;
//        for(byte b:bytes)
//        {
//            sum+=b;
//        }
//        return sum;
    }
 
    public static int sum(byte[] bytes)
    {
        return sum(bytes,0,bytes.length);
    }
    
    public static int getInt32(byte[] bytes,int start){  
        int result = 0;
        int a = (bytes[0+start] & 0xff) << 24;
        int b = (bytes[1+start] & 0xff) << 16;  
        int c = (bytes[2+start] & 0xff) << 8;  
        int d = (bytes[3+start] & 0xff);  
        result = a | b | c | d;
        return result;  
    }  
}