ISOUtils.java
2.07 KB
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
package com.glsx.pushsdk.tools;
public class ISOUtils {
public static final String TAG = "ISOUtils";
public static byte[] short2Bytes(short value) {
byte[] byteArrayData = new byte[2];
byteArrayData[0] = (byte) (value & 0xff);
byteArrayData[1] = (byte) ((value >> 8) & 0xff);
return byteArrayData;
}
public static short bytes2Short(byte[] s) {
short r = 0;
for (int i = s.length - 1; i >= 0; i--) {
r <<= 8;
r |= (s[i] & 0x00ff);
}
return r;
}
// 字节长度专用转换
public static byte[] letShort2Bytes1(short value) {
byte[] byteArrayData = new byte[2];
byteArrayData[0] = (byte) ((value >> 8) & 0xff);
byteArrayData[1] = (byte) (value & 0xff);
return byteArrayData;
}
public static byte[] int2bytes(int n) {
byte[] ab = new byte[4];
ab[0] = (byte) (0xff & n);
ab[1] = (byte) ((0xff00 & n) >> 8);
ab[2] = (byte) ((0xff0000 & n) >> 16);
ab[3] = (byte) ((0xff000000 & n) >> 24);
return ab;
}
public static int bytes2Int(byte[] bytes) {
int addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
/**
* 字节长度专用转换
*
* @param n int
* @return 返回byte[]
*/
public static byte[] lenInt2bytes1(int n) {
byte[] ab = new byte[4];
ab[0] = (byte) ((0xff00 & n) >> 8);
ab[1] = (byte) (0xff & n);
ab[2] = (byte) ((0xff0000 & n) >> 16);
ab[3] = (byte) ((0xff000000 & n) >> 24);
return ab;
}
/**
* 字节长度专用转换
*
* @param bytes byte[]
* @return 返回int
*/
public static int lenBytes2Int(byte[] bytes) {
int addr = ((bytes[0] << 8) & 0xFF00);
addr |= bytes[1] & 0xFF;
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
}