修复HexUtil.isHexNumber()判断逻辑超出long的精度问题

This commit is contained in:
Looly 2022-11-24 13:33:12 +08:00
parent 26771b2853
commit 4e06f02610
3 changed files with 29 additions and 21 deletions

View File

@ -10,6 +10,7 @@
### 🐞Bug修复
* 【json 】 修复普通byte数组转JSONArray时的异常pr#875@Gitee
* 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题pr#874@Gitee
* 【core 】 修复HexUtil.isHexNumber()判断逻辑超出long的精度问题issue#I62H7K@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -27,18 +27,18 @@ public class HexUtil {
* @return 是否为16进制
*/
public static boolean isHexNumber(String value) {
final int index = (value.startsWith("-") ? 1 : 0);
if (value.startsWith("0x", index) || value.startsWith("0X", index) || value.startsWith("#", index)) {
try {
//noinspection ResultOfMethodCallIgnored
Long.decode(value);
} catch (NumberFormatException e) {
return false;
}
return true;
int index = (value.startsWith("-") ? 1 : 0);
if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
index += 2;
} else if (value.startsWith("#", index)) {
index ++;
}
return false;
try {
new BigInteger(value.substring(index), 16);
} catch (final NumberFormatException e) {
return false;
}
return true;
}
// ---------------------------------------------------------------------------------------------------- encode

View File

@ -14,17 +14,17 @@ public class HexUtilTest {
@Test
public void hexStrTest(){
String str = "我是一个字符串";
final String str = "我是一个字符串";
String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
String decodedStr = HexUtil.decodeHexStr(hex);
final String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
final String decodedStr = HexUtil.decodeHexStr(hex);
Assert.assertEquals(str, decodedStr);
}
@Test
public void issueI50MI6Test(){
String s = HexUtil.encodeHexStr("".getBytes(StandardCharsets.UTF_16BE));
final String s = HexUtil.encodeHexStr("".getBytes(StandardCharsets.UTF_16BE));
Assert.assertEquals("70df", s);
}
@ -40,27 +40,34 @@ public class HexUtilTest {
@Test
public void isHexNumberTest() {
String a = "0x3544534F444";
boolean isHex = HexUtil.isHexNumber(a);
Assert.assertTrue(isHex);
Assert.assertTrue(HexUtil.isHexNumber(a));
// https://gitee.com/dromara/hutool/issues/I62H7K
a = "0x0000000000000001158e460913d00000";
Assert.assertTrue(HexUtil.isHexNumber(a));
// 错误的
a = "0x0000001000T00001158e460913d00000";
Assert.assertFalse(HexUtil.isHexNumber(a));
}
@Test
public void decodeTest(){
String str = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
final String str = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
Assert.assertArrayEquals(HexUtil.decodeHex(str),
HexUtil.decodeHex(str.toUpperCase()));
}
@Test
public void formatHexTest(){
String hex = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
String formatHex = HexUtil.format(hex);
final String hex = "e8c670380cb220095268f40221fc748fa6ac39d6e930e63c30da68bad97f885d";
final 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);
}
@Test
public void decodeHexTest(){
String s = HexUtil.encodeHexStr("6");
final String s = HexUtil.encodeHexStr("6");
final String s1 = HexUtil.decodeHexStr(s);
Assert.assertEquals("6", s1);
}