mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
HexUtil#format方法增加prefix参数
This commit is contained in:
parent
5dd8d51f7a
commit
832235f5e8
@ -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修复
|
||||
|
||||
|
@ -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位加一个空格,类似于:
|
||||
* <pre>
|
||||
* e8 8c 67 03 80 cb 22 00 95 26 8f
|
||||
* </pre>
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user