MCPcopy Create free account
hub / github.com/SeanDragon/protools / ToolConvert

Class ToolConvert

common/src/main/java/pro/tools/data/text/ToolConvert.java:20–541  ·  view source on GitHub ↗

转换相关工具 @author SeanDragon

Source from the content-addressed store, hash-verified

18 * @author SeanDragon
19 */
20public final class ToolConvert {
21 private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
22
23 private ToolConvert() {
24 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
25 }
26
27 /**
28 * byteArr转hexString <p>例如:</p> bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
29 *
30 * @param bytes
31 * 字节数组
32 *
33 * @return 16进制大写字符串
34 */
35 public static String bytes2HexString(byte[] bytes) {
36 if (bytes == null) {
37 return null;
38 }
39 int len = bytes.length;
40 if (len <= 0) {
41 return null;
42 }
43 char[] ret = new char[len << 1];
44 for (int i = 0, j = 0; i < len; i++) {
45 ret[j++] = HEX_DIGITS[bytes[i] >>> 4 & 0x0f];
46 ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];
47 }
48 return new String(ret);
49 }
50
51 /**
52 * hexString转byteArr <p>例如:</p> hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
53 *
54 * @param hexString
55 * 十六进制字符串
56 *
57 * @return 字节数组
58 */
59 public static byte[] hexString2Bytes(String hexString) {
60 if (ToolStr.isSpace(hexString)) {
61 return null;
62 }
63 int len = hexString.length();
64 if (len % 2 != 0) {
65 hexString = "0" + hexString;
66 len = len + 1;
67 }
68 char[] hexBytes = hexString.toUpperCase().toCharArray();
69 byte[] ret = new byte[len >> 1];
70 for (int i = 0; i < len; i += 2) {
71 ret[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
72 }
73 return ret;
74 }
75
76 /**
77 * hexChar转int

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected