diff --git a/CHANGELOG.md b/CHANGELOG.md index 353f16247..fd07095ad 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,12 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.27(2024-02-22) +# 5.8.27(2024-02-26) ### 🐣新特性 * 【extra 】 FreemarkerEngine修改默认版本参数 * 【db 】 增加达梦数据库方言(pr#1178@Gitee) +* 【core 】 HexUtil#format方法增加prefix参数(issue#I93PU9@Gitee) ### 🐞Bug修复 diff --git a/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java index 8d9dda219..a40d12c41 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java @@ -364,12 +364,30 @@ public class HexUtil { * @param hexStr Hex字符串 * @return 格式化后的字符串 */ - public static String format(String hexStr) { + public static String format(final String hexStr) { + return format(hexStr, StrUtil.EMPTY); + } + + /** + * 格式化Hex字符串,结果为每2位加一个空格,类似于: + *
+ * e8 8c 67 03 80 cb 22 00 95 26 8f + *+ * + * @param hexStr Hex字符串 + * @param prefix 自定义前缀,如0x + * @return 格式化后的字符串 + */ + public static String format(final String hexStr, String prefix) { + if (null == prefix) { + prefix = StrUtil.EMPTY; + } + final int length = hexStr.length(); - final StringBuilder builder = StrUtil.builder(length + length / 2); - builder.append(hexStr.charAt(0)).append(hexStr.charAt(1)); + final StringBuilder builder = StrUtil.builder(length + length / 2 + (length / 2 * prefix.length())); + builder.append(prefix).append(hexStr.charAt(0)).append(hexStr.charAt(1)); for (int i = 2; i < length - 1; i += 2) { - builder.append(CharUtil.SPACE).append(hexStr.charAt(i)).append(hexStr.charAt(i + 1)); + builder.append(CharUtil.SPACE).append(prefix).append(hexStr.charAt(i)).append(hexStr.charAt(i + 1)); } return builder.toString(); } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java index af2c6de62..98c76a45e 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java @@ -72,6 +72,13 @@ public class HexUtilTest { Assert.assertEquals("e8 c6 70 38 0c b2 20 09 52 68 f4 02 21 fc 74 8f a6 ac 39 d6 e9 30 e6 3c 30 da 68 ba d9 7f 88 5d", formatHex); } + @Test + public void formatHexTest2(){ + final String hex = "e8c670380cb220095268f40221fc748fa6"; + final String formatHex = HexUtil.format(hex, "0x"); + Assert.assertEquals("0xe8 0xc6 0x70 0x38 0x0c 0xb2 0x20 0x09 0x52 0x68 0xf4 0x02 0x21 0xfc 0x74 0x8f 0xa6", formatHex); + } + @Test public void decodeHexTest(){ final String s = HexUtil.encodeHexStr("6"); diff --git a/hutool-db/src/test/resources/config/db.setting b/hutool-db/src/test/resources/config/db.setting index bcef23fbb..e5b659cd4 100644 --- a/hutool-db/src/test/resources/config/db.setting +++ b/hutool-db/src/test/resources/config/db.setting @@ -74,6 +74,6 @@ remarks = true [dm] url = jdbc:dm://127.0.0.1:30236/schema=dm8_test user = SYSDBA -pass = SYSDBA001 +pass = 123456789 remarks = true