-
Notifications
You must be signed in to change notification settings - Fork 803
/
ConvertUtils.java
369 lines (338 loc) · 8.61 KB
/
ConvertUtils.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.jingewenku.abrahamcaijin.commonutil;
/**
* @Description:主要功能: 类型转换类
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: AbrahamCaiJin
* @date: 2017年05月16日 15:26
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class ConvertUtils {
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private ConvertUtils() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 十六进制字符串转换为byte数组
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
/**
* char转换为byte数组
* @param c
* @return
*/
public static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 16进制转化为数字
* @param ch 16进制
* @param index 索引
* @return 转化结果
* @throws Exception 转化失败异常
*/
private static int toDigit(final char ch, final int index)
throws Exception {
final int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new Exception("Illegal hexadecimal character " + ch
+ " at index " + index);
}
return digit;
}
/**
* bytes数组转16进制String
* @param data bytes数组
* @param toDigits DIGITS_LOWER或DIGITS_UPPER
* @return 转化结果
*/
private static String bytes2Hex(final byte[] data, final char[] toDigits) {
final int l = data.length;
final char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return new String(out);
}
/**
* byte数组转换为十六进制字符串
*
* @param b
* @return
*/
public static String bytesToHexString(byte[] b) {
if (b.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < b.length; i++) {
int value = b[i] & 0xFF;
String hv = Integer.toHexString(value);
if (hv.length() < 2) {
sb.append(0);
}
sb.append(hv);
}
return sb.toString();
}
/**
* int转换为byte数组
*
* @param res
* @return
*/
public static byte[] intToByte(int res) {
byte[] targets = new byte[4];
targets[0] = (byte) (res & 0xff);// 最低位
targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
return targets;
}
/**
* byte数组转换为int
*
* @param res
* @return
*/
public static int byteToInt(byte[] res) {
// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
int targets = (res[3] & 0xff) | ((res[2] << 8) & 0xff00) | ((res[1] << 16) & 0xff0000) | ((res[0] << 24) & 0xff000000);
return targets;
}
/**
* 保留几位小数
*/
public static String saveDecimals(int cnt, double value) {
if (cnt == 2)
return String.format("%.02f", value);
else if (cnt == 1)
return String.format("%.01f", value);
else
return String.format("%.0f", value);
}
/**
* null转String
* @param str
* @return
*/
public static String nullOfString(String str) {
if (str == null) {
str = "";
}
return str;
}
/**
* String转Byte
* @param str
* @return
*/
public static byte stringToByte(String str) {
byte b = 0;
if (str != null) {
try {
b = Byte.parseByte(str);
} catch (Exception e) {
}
}
return b;
}
/**
* String转Boolean
* @param str
* @return
*/
public static boolean stringToBoolean(String str) {
if (str == null) {
return false;
} else {
if (str.equals("1")) {
return true;
} else if (str.equals("0")) {
return false;
} else {
try {
return Boolean.parseBoolean(str);
} catch (Exception e) {
return false;
}
}
}
}
/**
* String转Int
* @param str
* @return
*/
public static int stringToInt(String str) {
int i = 0;
if (str != null) {
try {
i = Integer.parseInt(str.trim());
} catch (Exception e) {
i = 0;
}
} else {
i = 0;
}
return i;
}
/**
* String转Short
* @param str
* @return
*/
public static short stringToShort(String str) {
short i = 0;
if (str != null) {
try {
i = Short.parseShort(str.trim());
} catch (Exception e) {
i = 0;
}
} else {
i = 0;
}
return i;
}
/**
* String转Double
* @param str
* @return
*/
public static double stringToDouble(String str) {
double i = 0;
if (str != null) {
try {
i = Double.parseDouble(str.trim());
} catch (Exception e) {
i = 0;
}
} else {
i = 0;
}
return i;
}
/**
* Int转String
* @param i
* @return
*/
public static String intToString(int i) {
String str = "";
try {
str = String.valueOf(i);
} catch (Exception e) {
str = "";
}
return str;
}
/**
* Double转Long
* @param d
* @return
*/
public static long doubleToLong(double d) {
long lo = 0;
try {
//double转换成long前要过滤掉double类型小数点后数据
lo = Long.parseLong(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
} catch (Exception e) {
lo = 0;
}
return lo;
}
/**
* Double转Int
* @param d
* @return
*/
public static int doubleToInt(double d) {
int i = 0;
try {
//double转换成long前要过滤掉double类型小数点后数据
i = Integer.parseInt(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
} catch (Exception e) {
i = 0;
}
return i;
}
/**
* Long转Double
* @param d
* @return
*/
public static double longToDouble(long d) {
double lo = 0;
try {
lo = Double.parseDouble(String.valueOf(d));
} catch (Exception e) {
lo = 0;
}
return lo;
}
/**
* Long转Int
* @param d
* @return
*/
public static int longToInt(long d) {
int lo = 0;
try {
lo = Integer.parseInt(String.valueOf(d));
} catch (Exception e) {
lo = 0;
}
return lo;
}
/**
* String转Long
* @param str
* @return
*/
public static long stringToLong(String str) {
Long li = new Long(0);
try {
li = Long.valueOf(str);
} catch (Exception e) {
//li = new Long(0);
}
return li.longValue();
}
/**
* Long转String
* @param li
* @return
*/
public static String longToString(long li) {
String str = "";
try {
str = String.valueOf(li);
} catch (Exception e) {
}
return str;
}
}