18045010223
8 天以前 afe371d39a054b2f2a9e5875b945584eec8a8141
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
package cn.org.hentai.jtt1078.util;
 
/**
 * Created by matrixy on 2017/8/22.
 */
public final class ByteUtils
{
    public static String toHexString(byte[] bytes, int offset, int length) {
        StringBuilder sb = new StringBuilder(length * 3);
        for (int i = offset; i < offset + length && i < bytes.length; i++) {
            sb.append(String.format("%02X ", bytes[i]));
        }
        return sb.toString().trim();
    }
 
    public static byte[] parse(String hexString)
    {
        String[] hexes = hexString.split(" ");
        byte[] data = new byte[hexes.length];
        for (int i = 0; i < hexes.length; i++) data[i] = (byte)(Integer.parseInt(hexes[i], 16) & 0xff);
        return data;
    }
 
    public static synchronized void dump(byte[] data)
    {
        dump(data, data.length);
    }
 
    public static synchronized void dump(byte[] data, int len)
    {
        for (int i = 0, l = len; i < l && i < data.length; )
        {
            String ascii = "";
            int k = 0, f = 0;
            for (; k < 16 && k < data.length; k++)
            {
                if (k + i < l)
                {
                    f++;
                    byte d = data[i + k];
                    String hex = Integer.toHexString(d & 0xff).toUpperCase();
                    if (hex.length() == 1) hex = "0" + hex;
                    if (d >= 0x20 && d < 127) ascii += (char)d;
                    else ascii += '.';
                    System.out.print(hex);
                }
                else
                {
                    System.out.print(' ');
                    System.out.print(' ');
                }
 
                if (k % 4 == 3) System.out.print("   ");
                else System.out.print(' ');
            }
            i += f;
            System.out.println(ascii);
        }
    }
 
    public static String toString(byte[] data)
    {
        if (null == data) return "";
        return toString(data, data.length);
    }
 
    public static String toString(byte[] buff, int length)
    {
        StringBuffer sb = new StringBuffer(length * 2);
        for (int i = 0; i < buff.length && i < length; i++)
        {
            if ((buff[i] & 0xff) < 0x10) sb.append('0');
            sb.append(Integer.toHexString(buff[i] & 0xff).toUpperCase());
            sb.append(' ');
        }
        return sb.toString();
    }
 
    public static boolean getBit(int val, int pos)
    {
        return getBit(new byte[] {
                (byte)((val >> 0) & 0xff),
                (byte)((val >> 8) & 0xff),
                (byte)((val >> 16) & 0xff),
                (byte)((val >> 24) & 0xff)
        }, pos);
    }
 
    public static int reverse(int val)
    {
        byte[] bytes = toBytes(val);
        byte[] ret = new byte[4];
        for (int i = 0; i < 4; i++) ret[i] = bytes[3 - i];
        return toInt(ret);
    }
 
    public static byte[] toLEBytes(int val)
    {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++)
        {
            bytes[3 - i] = (byte)(val >> ((3 - i) * 8) & 0xff);
        }
        return bytes;
    }
 
    public static byte[] toLEBytes(short s)
    {
        byte[] bytes = new byte[2];
        bytes[0] = (byte)(s & 0xff);
        bytes[1] = (byte)((s >> 8) & 0xff);
        return bytes;
    }
 
    public static int toInt(byte[] bytes)
    {
        int val = 0;
        for (int i = 0; i < 4; i++) val |= (bytes[i] & 0xff) << ((3 - i) * 8);
        return val;
    }
 
    public static byte[] toBytes(int val)
    {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++)
        {
            bytes[i] = (byte)(val >> ((3 - i) * 8) & 0xff);
        }
        return bytes;
    }
 
    public static byte[] toBytes(long val)
    {
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++)
        {
            bytes[i] = (byte)(val >> ((7 - i) * 8) & 0xff);
        }
        return bytes;
    }
 
    public static int getInt(byte[] data, int offset, int length)
    {
        int val = 0;
        for (int i = 0; i < length; i++) val |= (data[offset + i] & 0xff) << ((length - i - 1) * 8);
        return val;
    }
 
    public static long getLong(byte[] data, int offset, int length)
    {
        long val = 0;
        for (int i = 0; i < length; i++) val |= ((long)data[offset + i] & 0xff) << ((length - i - 1) * 8);
        return val;
    }
 
    public static boolean getBit(byte[] data, int pos)
    {
        return ((data[pos / 8] >> (pos % 8)) & 0x01) == 0x01;
    }
 
    public static byte[] concat(byte[]...byteArrays)
    {
        int len = 0, index = 0;
        for (int i = 0; i < byteArrays.length; i++) len += byteArrays[i].length;
        byte[] buff = new byte[len];
        for (int i = 0; i < byteArrays.length; i++)
        {
            System.arraycopy(byteArrays[i], 0, buff, index, byteArrays[i].length);
            index += byteArrays[i].length;
        }
        return buff;
    }
 
    public static boolean compare(byte[] data1, byte[] data2)
    {
        if (data1.length != data2.length) return false;
        for (int i = 0; i < data1.length; i++)
            if ((data1[i] & 0xff) != (data2[i] & 0xff)) return false;
        return true;
    }
 
    // 相当于(short *) byte_pointer的效果
    public static short[] toShortArray(byte[] src)
    {
        short[] dst = new short[src.length / 2];
        for (int i = 0, k = 0; i < src.length; )
        {
            dst[k++] = (short)((src[i++] & 0xff) | ((src[i++] & 0xff) << 8));
        }
        return dst;
    }
 
    // 相当于(char *) short_pointer的效果
    public static byte[] toByteArray(short[] src)
    {
        byte[] dst = new byte[src.length * 2];
        for (int i = 0, k = 0; i < src.length; i++)
        {
            dst[k++] = (byte)(src[i] & 0xff);
            dst[k++] = (byte)((src[i] >> 8) & 0xff);
        }
        return dst;
    }
}