Utilty.java
1.38 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
package com.fh.party;
public class Utilty {
private static Utilty instance = new Utilty();
public static Utilty getInstance() {
return instance;
}
public static final int MIN_MID_VALUE = 1;
public static final int MAX_MID_VALUE = 65535;
public int bytes2Int(byte[] b, int start, int length) {
int sum = 0;
int end = start + length;
for (int k = start; k < end; k++) {
int n = ((int) b[k]) & 0xff;
n <<= (--length) * 8;
sum += n;
}
return sum;
}
public byte[] int2Bytes(int value, int length) {
byte[] b = new byte[length];
for (int k = 0; k < length; k++) {
b[length - k - 1] = (byte) ((value >> 8 * k) & 0xff);
}
return b;
}
public boolean isValidofMid(int mId) {
if (mId < MIN_MID_VALUE || mId > MAX_MID_VALUE) {
return false;
}
return true;
}
public static String parseByte2HexStr(byte[] buf) {
if (null == buf) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
}