This commit is contained in:
Looly 2022-11-24 12:29:08 +08:00
parent 98307df486
commit e85bd4ada4
2 changed files with 21 additions and 14 deletions

View File

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

View File

@ -40,9 +40,16 @@ public class HexUtilTest {
@Test
public void isHexNumberTest() {
final String a = "0x3544534F444";
final boolean isHex = HexUtil.isHexNumber(a);
Assert.assertTrue(isHex);
String a = "0x3544534F444";
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