From 4f27e2a21aa7c0cbd07447b43fc3b83fd1525f88 Mon Sep 17 00:00:00 2001
From: kongdeqiang <123456>
Date: 星期一, 10 七月 2023 15:07:25 +0800
Subject: [PATCH] 修改
---
src/main/java/com/boying/util/StringUtil.java | 106 +++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 95 insertions(+), 11 deletions(-)
diff --git a/src/main/java/com/boying/util/StringUtil.java b/src/main/java/com/boying/util/StringUtil.java
index c99d6cc..929bd9c 100644
--- a/src/main/java/com/boying/util/StringUtil.java
+++ b/src/main/java/com/boying/util/StringUtil.java
@@ -5,6 +5,12 @@
public class StringUtil {
+ /** 16杩涘埗涓殑瀛楃闆� */
+ private static final String HEX_CHAR = "0123456789ABCDEF";
+
+ /** 16杩涘埗涓殑瀛楃闆嗗搴旂殑瀛楄妭鏁扮粍 */
+ private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes();
+
public static boolean isNullOrEmpty(String str) {
if (null == str || "".equalsIgnoreCase(str.trim())
|| "null".equals(str.trim())) {
@@ -31,6 +37,27 @@
return s2;
}
+ public static byte[] toBytes(String strs){
+ String[] split = strs.split("-");
+ byte[] bytes = new byte[split.length];
+ for (int i = 0; i < split.length; i++) {
+ String s = split[i];
+ bytes[i] = (byte) Byte.parseByte(s);
+ }
+ return bytes;
+ }
+
+ public static byte[] hex2Byte(String hex) {
+ System.out.println();
+ String[] parts = hex.split("-");
+ byte[] bytes = new byte[parts.length];
+ for (int i = 0; i < parts.length; i++) {
+ bytes[i] = (byte) (Integer.parseInt(parts[i],10));
+ System.out.print(bytes[i]+ " ");
+ }
+ return bytes;
+ }
+
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
@@ -51,7 +78,6 @@
}
System.out.print(hex.toUpperCase() + " ");
}
- System.out.println("");
}
/**
@@ -81,11 +107,11 @@
if (null == src || 0 == src.length()) {
return null;
}
- String s1 = src.replaceAll("-", "");
- byte[] ret = new byte[s1.length() / 2];
- byte[] tmp = s1.getBytes();
+ // String s1 = src.replaceAll("-", "");
+ byte[] ret = new byte[src.length() / 2];
+ byte[] tmp = src.getBytes();
for (int i = 0; i < (tmp.length / 2); i++) {
- ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
+ ret[i] = (byte)uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
@@ -105,12 +131,70 @@
return ret;
}
- public static void main(String[] args) {
- String s = "00-64-FF-FF-6E-78-00-04-00-01-01-02-00-FF-00-00-00-09-E4-BA-AC-41-38-38-38-38-38-0D-01-01-01-02-00-FF-00-00-00-09-E4-B8-B4-E6-97-B6-E8-BD-A6-0D-02-01-01-02-00-FF-00-00-00-0C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-0D-03-01-01-02-00-FF-00-00-00-09-E4-BD-99-E4-BD-8D-3A-37-34-00-0A-20-E4-B8-B4-E6-97-B6-E8-BD-A6-2C-E4-BA-AC-41-38-38-38-38-38-2C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-00-6B-E7";
- String s1 = s.replaceAll("-", "");
- byte[] bytes = HexString2Bytes(s1);
- for (byte aByte : bytes) {
- System.out.print(aByte + " ");
+ /**
+ * 10杩涘埗瀛楄妭鏁扮粍杞崲涓�16杩涘埗瀛楄妭鏁扮粍
+ *
+ * byte鐢ㄤ簩杩涘埗琛ㄧず鍗犵敤8浣嶏紝16杩涘埗鐨勬瘡涓瓧绗﹂渶瑕佺敤4浣嶄簩杩涘埗浣嶆潵琛ㄧず锛屽垯鍙互鎶婃瘡涓猙yte
+ * 杞崲鎴愪袱涓浉搴旂殑16杩涘埗瀛楃锛屽嵆鎶奲yte鐨勯珮4浣嶅拰浣�4浣嶅垎鍒浆鎹㈡垚鐩稿簲鐨�16杩涘埗瀛楃锛屽啀鍙栧搴�16杩涘埗瀛楃鐨勫瓧鑺�
+ *
+ * @param b 10杩涘埗瀛楄妭鏁扮粍
+ * @return 16杩涘埗瀛楄妭鏁扮粍
+ */
+ public static byte[] byte2hex(byte[] b) {
+ int length = b.length;
+ byte[] b2 = new byte[length << 1];
+ int pos;
+ for(int i=0; i<length; i++) {
+ pos = 2*i;
+ b2[pos] = HEX_STRING_BYTE[(b[i] & 0xf0) >> 4];
+ b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f];
}
+ return b2;
+ }
+
+ /**
+ * 16杩涘埗瀛楄妭鏁扮粍杞崲涓�10杩涘埗瀛楄妭鏁扮粍
+ *
+ * 涓や釜16杩涘埗瀛楄妭瀵瑰簲涓�涓�10杩涘埗瀛楄妭锛屽垯灏嗙涓�涓�16杩涘埗瀛楄妭瀵瑰簲鎴�16杩涘埗瀛楃琛ㄤ腑鐨勪綅缃�(0~15)骞跺悜宸︾Щ鍔�4浣嶏紝
+ * 鍐嶄笌绗簩涓�16杩涘埗瀛楄妭瀵瑰簲鎴�16杩涘埗瀛楃琛ㄤ腑鐨勪綅缃�(0~15)杩涜鎴栬繍绠楋紝鍒欏緱鍒板搴旂殑10杩涘埗瀛楄妭
+ * @param b 10杩涘埗瀛楄妭鏁扮粍
+ * @return 16杩涘埗瀛楄妭鏁扮粍
+ */
+ public static byte[] hex2byte(byte[] b) {
+ if(b.length%2 != 0) {
+ throw new IllegalArgumentException("byte array length is not even!");
+ }
+
+ int length = b.length >> 1;
+ byte[] b2 = new byte[length];
+ int pos;
+ for(int i=0; i<length; i++) {
+ pos = i << 1;
+ b2[i] = (byte) (HEX_CHAR.indexOf( b[pos] ) << 4 | HEX_CHAR.indexOf( b[pos+1] ) );
+ }
+ return b2;
+ }
+
+ /**
+ * 灏�16杩涘埗瀛楄妭鏁扮粍杞垚10杩涘埗瀛楃涓�
+ * @param b 16杩涘埗瀛楄妭鏁扮粍
+ * @return 10杩涘埗瀛楃涓�
+ */
+ public static String hex2Str(byte[] b) {
+ return new String(hex2byte(b));
+ }
+
+ /**
+ * 灏�10杩涘埗瀛楄妭鏁扮粍杞垚16杩涘埗瀛楃涓�
+ * @param b 10杩涘埗瀛楄妭鏁扮粍
+ * @return 16杩涘埗瀛楃涓�
+ */
+ public static String byte2HexStr(byte[] b) {
+ return Integer.toHexString(Integer.parseInt(new String(b)));
+ }
+
+
+ public static void main(String[] args) {
+
}
}
--
Gitblit v1.9.1