-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
91 lines (79 loc) · 1.99 KB
/
Utils.java
File metadata and controls
91 lines (79 loc) · 1.99 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
82
83
84
85
86
87
88
89
90
91
import java.io.*;
import java.util.*;
public class Utils {
public static String byteToHex(byte[] dataB){
String result = "";
for(byte b : dataB){
result += String.format("%02X", b);
}
return result;
}
public static String hexToChar(String hex){
String result = "";
String tmp = "";
for(int i = 0; i < hex.length(); i+=2){
tmp = hex.substring(i, i + 2);
result += (char)Integer.parseInt(tmp, 16);
}
return result;
}
public static byte[] hexToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static long byteToIntBE(byte[] bytes){
String hex = "";
for(int i = 0; i <= bytes.length - 1; i++){
hex += String.format("%02X", bytes[i]);
}
long i = Long.parseLong(hex,16);
return i;
}
public static int byteToIntLE(byte[] bytes){
String hex = "";
for(int i = bytes.length - 1; i >= 0; i--){
hex += String.format("%02X", bytes[i]);
}
int i = Integer.parseInt(hex,16);
return i;
}
public static Date addSeconds(Date date, int seconds) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.SECOND, seconds);
return cal.getTime();
}
public static String addTab(String text, int numberOfTab){
String newString = "";
String[] parts = text.split("\n");
int j = 0;
for (int i = 0; i < parts.length; i++){
j = 0;
while(j < numberOfTab){
newString += "\t";
j++;
}
newString += parts[i] + "\n";
}
return newString;
}
public static String byteToIP(byte[] dataB){
String result = "";
for(byte b : dataB){
result += String.format("%d.", b & 255);
}
return result.substring(0, result.length() - 1);
}
public static String byteToMac(byte[] dataB){
String result = "";
for(byte b : dataB){
result += String.format("%02X:", b);
}
return result.substring(0, result.length() - 1);
}
}