add method

This commit is contained in:
Looly 2021-07-02 11:21:36 +08:00
parent e7a5295460
commit 761a0e0940
3 changed files with 35 additions and 4 deletions

View File

@ -8,6 +8,7 @@
### 🐣新特性 ### 🐣新特性
* 【crypto 】 SmUtil.sm4统一返回类型issue#I3YKD4@Gitee * 【crypto 】 SmUtil.sm4统一返回类型issue#I3YKD4@Gitee
* 【core 】 修改MapUtil.get传入null返回默认值而非nullissue#I3YKBC@Gitee * 【core 】 修改MapUtil.get传入null返回默认值而非nullissue#I3YKBC@Gitee
* 【core 】 HexUtil增加hexToLong、hexToIntissue#I3YQEV@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复RadixUtil.decode非static问题issue#I3YPEH@Gitee * 【core 】 修复RadixUtil.decode非static问题issue#I3YPEH@Gitee

View File

@ -148,7 +148,7 @@ public class HexUtil {
if (StrUtil.isEmpty(hexStr)) { if (StrUtil.isEmpty(hexStr)) {
return hexStr; return hexStr;
} }
return decodeHexStr(hexStr.toCharArray(), charset); return StrUtil.str(decodeHex(hexStr), charset);
} }
/** /**
@ -197,11 +197,12 @@ public class HexUtil {
} }
hexData = StrUtil.cleanBlank(hexData); hexData = StrUtil.cleanBlank(hexData);
int len = hexData.length();
final int len = hexData.length();
if ((len & 0x01) != 0) { if ((len & 0x01) != 0) {
throw new UtilException("Odd number of characters."); hexData = "0" + hexData;
len = hexData.length();
// throw new UtilException("Odd number of characters.");
} }
final byte[] out = new byte[len >> 1]; final byte[] out = new byte[len >> 1];
@ -327,6 +328,17 @@ public class HexUtil {
return Integer.toHexString(value); return Integer.toHexString(value);
} }
/**
* 16进制字符串转为int
*
* @param value 16进制字符串
* @return 16进制字符串int值
* @since 5.7.4
*/
public static int hexToInt(String value) {
return Integer.parseInt(value, 16);
}
/** /**
* 转为16进制字符串 * 转为16进制字符串
* *
@ -338,6 +350,17 @@ public class HexUtil {
return Long.toHexString(value); return Long.toHexString(value);
} }
/**
* 16进制字符串转为long
*
* @param value 16进制字符串
* @return long值
* @since 5.7.4
*/
public static long hexToLong(String value) {
return Long.parseLong(value, 16);
}
/** /**
* 将byte值转为16进制并添加到{@link StringBuilder} * 将byte值转为16进制并添加到{@link StringBuilder}
* *

View File

@ -49,4 +49,11 @@ public class HexUtilTest {
String formatHex = HexUtil.format(hex); String formatHex = HexUtil.format(hex);
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); 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 decodeHexTest(){
String s = HexUtil.encodeHexStr("6");
final String s1 = HexUtil.decodeHexStr(s);
Assert.assertEquals("6", s1);
}
} }