forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.java
More file actions
49 lines (45 loc) · 1.32 KB
/
Copy pathHelper.java
File metadata and controls
49 lines (45 loc) · 1.32 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
package DNA;
/**
* Byte Handle Helper
*
* @author 12146
* @since JDK1.8
*/
public class Helper {
public static String getbyteStr(byte[] bs) {
StringBuilder sb = new StringBuilder();
for(byte b: bs) {
sb.append(" ").append(Byte.toUnsignedInt(b));
}
return sb.substring(1);
}
public static byte[] reverse(byte[] v) {
byte[] result = new byte[v.length];
for (int i = 0; i < v.length; i++) {
result[i] = v[v.length - i - 1];
}
return result;
}
public static byte[] hexToBytes(String value) {
if (value == null || value.length() == 0) {
return new byte[0];
}
if (value.length() % 2 == 1) {
throw new IllegalArgumentException();
}
byte[] result = new byte[value.length() / 2];
for (int i = 0; i < result.length; i++) {
result[i] = (byte) Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
}
return result;
}
public static String toHexString(byte[] value) {
StringBuilder sb = new StringBuilder();
for (byte b : value) {
int v = Byte.toUnsignedInt(b);
sb.append(Integer.toHexString(v >>> 4));
sb.append(Integer.toHexString(v & 0x0f));
}
return sb.toString();
}
}