-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitUtils.java
More file actions
64 lines (55 loc) · 1.24 KB
/
BitUtils.java
File metadata and controls
64 lines (55 loc) · 1.24 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
package common.android.fiot.androidcommon;
/**
* Created by caoxuanphong on 3/23/17.
*/
public class BitUtils {
/**
* Covert Integer to string of binary
*
* @param i
* @return
*/
public static String toBinaryString(int i) {
return Integer.toBinaryString(i);
}
/**
* Convert Binary string into Integer
*
* @param b
* @return
*/
public static int binaryStringToInt(String b) {
return Integer.parseInt(b, 2);
}
/**
* Convert Binary string into Hex string
*
* @param b
* @return
*/
public static String binaryStringToHexString(String b) {
return Integer.toHexString(Integer.parseInt(b, 2));
}
/**
* Get value of Integer with bit
*
* @param i
* @param startPos
* @param length
* @return
*/
public static String getBit(int i, int startPos, int length) {
if (startPos < 0 || length < 0) {
return null;
}
String b = "";
int j = 0;
do {
int k = (i >> startPos) & 1;
b += k;
j ++;
startPos ++;
} while (j < length);
return new StringBuilder(b).reverse().toString();
}
}