liurunyu
2023-11-12 21eb47b061d16056f37eee47928c7fe629b63061
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.dy.common.util;
 
@SuppressWarnings("unuseed")
public class ByteUtilUnsigned {
    /**
     * 大端模式《数据低位在数组高字节》
     * 无符号int类型转换成4位byte数组
     * java没有无符号整型数据,只有有符号整数,取值范围是-2147483648~2147483647
     * C有无符号整型数据,取值范围是0到4294967295,已经超出了java的有符号整数上限,所以只能用java的long型表示无符号整数
     * @value bs byte[]
     * @value value int int类型的参数
     * @value from int
     * @throws Exception 异常
     */
    public static void int2Bytes_BE(byte[] bs, long value, int from)throws Exception {
        int len = 4 ;
        Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE * 2 + 1) ;
        Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE * 2) ;
        if(value < minIntUnsigned || value > maxIntUnsigned ){
            throw new Exception("数据" + value + "超出了无符号Int型的取值范围(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ;
        }
 
        int temp ;
        if(value > Integer.MAX_VALUE){
            temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ;
        }else{
            temp = Long.valueOf(value).intValue() ;
        }
        boolean b = isOutOfArrLength(bs.length, (from - 1) + len);
        if (!b) {
            for (int i = (len - 1); i >= 0; i--) {
                bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将最低位保存在高字节
                temp = temp >> 8; // 向右移8位
            }
        } else {
            throw new Exception("int2Bytes时数组越界");
        }
    }
    
 
    /**
     * 小端模式《数据低位在数组低字节》
     * 与方法int2Bytes算法一样,只是把顺序反过来
     * @value bs byte[]
     * @value value int int类型的参数
     * @value from int
     */
    public static void int2Bytes_LE(byte[] bs, long value, int from)throws Exception {
        int len = 4 ;
        Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE) * 2 + 1;
        Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE) * 2 ;
        if(value < minIntUnsigned || value > maxIntUnsigned ){
            throw new Exception("数据" + value + "超出了无符号Int型的取值范围(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ;
        }
 
        int temp ;
        if(value > Integer.MAX_VALUE){
            temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ;
        }else{
            temp = Long.valueOf(value).intValue() ;
        }
        boolean b = isOutOfArrLength(bs.length, (from - 1) + len);
        if (!b) {
            for (int i = 0; i > len ; i++) {
                bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();// 将最低位保存在低字节
                temp = temp >> 8; // 向右移8位
            }
        } else {
            throw new Exception("int2Bytes时数组越界");
        }
    }
 
    /**
     * 大端模式《数据低位在数组高字节》
     * 4位字节数组转换为整型
     * @param bs 字节数组
     * @param from 起始位置
     * @return 结果
     */
    public static long bytes2Int_BE(byte[] bs, int from) throws Exception {
        boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
        if (!b) {
            long s = 0;
            long s0 = bs[from + 0] & 0xFF ;// 数据的最高位在低字节
            long s1 = bs[from + 1] & 0xFF ;
            long s2 = bs[from + 2] & 0xFF ;
            long s3 = bs[from + 3] & 0xFF ;
 
            // 最低位S3不变
            s0 <<= 24;
            s1 <<= 16;
            s2 <<= 8;
            s = s0 | s1 | s2 | s3;
            if(s < 0){
                //s = Integer.MAX_VALUE -s ;
                s = Integer.MAX_VALUE * 2 + 1 + s + 1 ;
            }
            return s;
        } else {
            throw new Exception("byte2Int时数组越界");
        }
    }
 
    /**
     * 小端模式《数据低位在数组低字节》
     * 与方法bytes2Int算法一样,只是把顺序反过来
     * @param bs 字节数组
     * @param from 字节数组起始位置
     * @return
     */
    public static long bytes2Int_LE(byte[] bs, int from) throws Exception {
        boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
        if (!b) {
            long s = 0;
            long s0 = bs[from + 0] & 0xFF ;// 数据的最低位在低字节
            long s1 = bs[from + 1] & 0xFF ;
            long s2 = bs[from + 2] & 0xFF ;
            long s3 = bs[from + 3] & 0xFF ;
 
            // S0不变
            s1 <<= 8;
            s2 <<= 16;
            s3 <<= 24;
            s = s0 | s1 | s2 | s3;
            if(s < 0){
                //s = Integer.MAX_VALUE -s ;
                s = Integer.MAX_VALUE * 2 + 1 + s + 1 ;
            }
            return s;
        } else {
            throw new Exception("byte2Int时数组越界");
        }
    }
    /**
     * 大端模式《数据低位在数组高字节》
     * 无符号short类型转换成2位byte数组
     * java没有无符号短整型数据,只有有符号短整数,取值范围是-32768~32767
     * 若模拟无符号短整型数据,取值范围是0到65535 ,已经超出了java的有符号整数上限,所以只能用java的Int型表示无符号整数
     * @value bs byte[]
     * @value value int int类型的参数
     * @value from int
     */
    public static void short2Bytes_BE(byte[] bs, int value, int from)throws Exception {
        int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1;
        int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ;
        if(value < minShortUnsigned || value > maxShortUnsigned ){
            throw new Exception("数据" + value + "超出了无符号short型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ;
        }
        short temp = 0 ;
        if(value > Short.MAX_VALUE){
            temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ;
        }else{
            temp = Integer.valueOf(value).shortValue() ;
        }
        boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
        if (!b) {
            for (int i = 1; i >= 0; i--) {
                bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将最低位保存在高字节
                temp = (short)(temp >> 8); // 向右移8位
            }
        } else {
            throw new Exception("short2Bytes时数组越界");
        }
    }
    /**
     * 小端模式《数据低位在数组低字节》
     * 与方法short2Bytes算法一样,只是把顺序反过来
     * @value bs byte[]
     * @value value int int类型的参数
     * @value from int
     */
    public static void short2Bytes_LE(byte[] bs, int value, int from)throws Exception {
        int len = 2 ;
        int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1;
        int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ;
        if(value < minShortUnsigned || value > maxShortUnsigned ){
            throw new Exception("数据" + value + "超出了无符号short型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ;
        }
        short temp = 0 ;
        if(value > Short.MAX_VALUE){
            temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ;
        }else{
            temp = Integer.valueOf(value).shortValue() ;
        }
        boolean b = isOutOfArrLength(bs.length, (from - 1) + len);
        if (!b) {
            for (int i = 0; i < len; i++) {
                bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//将数据低位保存在数据低字节
                temp = (short)(temp >> 8); // 向右移8位
            }
        } else {
            throw new Exception("short2Bytes时数组越界");
        }
    }
 
    /**
     * 大端模式《数据低位在数组高字节》
     * 2位字节数组转换为短整型
     * @param bs 字节数组
     * @param from 字节数组起始位置
     * @return 结果
     */
    public static int bytes2Short_BE(byte[] bs, int from) throws Exception {
        boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
        if (!b) {
            int s = 0;
            int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 最低位
            int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue();
 
            // 最低位S1不变
            s0 <<= 8;
            s = s0 | s1 ;
            if(s < 0){
                s = (Short.MAX_VALUE * 2 + 1) + s + 1;
            }
            return s;
        } else {
            throw new Exception("bytes2Short时数组越界");
        }
    }
 
    /**
     * 小端模式《数据低位在数组低字节》
     * 与方法bytes2Short算法一样,只是把顺序反过来
     * @param bs 字节数组
     * @param from 字节数组起始位置
     * @return 结果
     */
    public static int bytes2Short_LE(byte[] bs, int from) throws Exception {
        boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
        if (!b) {
            int s = 0;
            int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 小下标字节转数据低位
            int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue();// 大下标字节转数据高位
 
            // 最低位S0不变
            s1 <<= 8;
            s = s1 | s0 ;
            if(s < 0){
                s = (Short.MAX_VALUE * 2 + 1) + s + 1;
            }
            return s;
        } else {
            throw new Exception("bytes2Short时数组越界");
        }
    }
    
 
    /**
     * 1位字节数组转换为短整型
     * @param bs 字节数组
     * @param index 字节数组起始位置
     * @return 结果
     */
    public static short byte2Byte(byte[] bs, int index) throws Exception {
        if (bs.length - 1 < index) {
            throw new Exception("byte2Short(byte[] bs, int index)时数组越界");
        } else {
            byte bv = (byte)(bs[index] & 0xff) ; 
            short s = 0 ;
            if(bv < 0){
                s = (short)(Byte.MAX_VALUE * 2 + 1 + bv + 1) ;
            }else{
                s = bv ;
            }
            return s ;
        }
    }
    /**
     * 无符号short类型转换成1位byte
     * java没有无符号短整型数据,只有有符号短整数,取值范围是-128~127
     * 若模拟无符号短整型数据,取值范围是0到255 ,已经超出了java的有符号整数上限,所以只能用java的short型表示无符号整数
     * @value bs byte[] 字节数组
     * @value value short short类型的参数
     * @value index 起始位置
     */
    public static void byte2Byte(byte[] bs, short value, int index)throws Exception {
        int maxShortUnsigned = Integer.valueOf(Byte.MAX_VALUE) * 2 + 1;
        int minShortUnsigned = Integer.valueOf(Byte.MIN_VALUE) * 2 ;
        if(value < minShortUnsigned || value > maxShortUnsigned ){
            throw new Exception("数据" + value + "超出了无符号byte型的取值范围(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ;
        }
        if (bs.length - 1 < index) {
            throw new Exception("byte2Byte(byte[] bs, short value, int index)时数组越界");
        } else {
            bs[index] = Integer.valueOf(value & 0xff).byteValue() ;
        }
    }
    
    /**
     * 判断所有字节是否为0xFF
     * @param bs  字节数组
     * @param index 起始位置
     * @param len 长度
     * @return 结果
     * @throws Exception
     */
    public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception {
        int count = 0 ;
        for(int i = index; i < index + len; i++){
            if(bs[i] == (byte)0xFF){
                count++ ;
            }
        }
        return count==len?true:false ;
    }
 
    /**
     * 判断数组下标是否越界
     * 
     * @value bsLength 数组总长度
     * @value toSite 数组偏移量
     * @return 结果
     */
    private static boolean isOutOfArrLength(int bsLength, int toSite) {
        if (bsLength > toSite) {
            return false;
        } else {
            return true;
        }
    }
 
    /*
    public static void main(String[] args) throws Exception{
//        int d = 123456; 
//        byte[] bs = new byte[4] ;
//        int2Bytes_BE(bs, d, 0) ;
//        System.out.println(ByteUtil.bytes2Hex(bs, false));
//        long dd = bytes2Int_BE(bs, 0) ;
//        System.out.println(dd);
//        
//        byte[] bb = new byte[1] ;
//        bb[0] = (byte)255 ;
//        short s = byte2Byte(bb, 0);
//        System.out.println(s);
        
        byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ;
        //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ;
        boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ;
        System.out.println(flag);
        Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0);
        System.out.println(s);
    }
    */
}