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

View File

@ -40,9 +40,16 @@ public class HexUtilTest {
@Test @Test
public void isHexNumberTest() { public void isHexNumberTest() {
final String a = "0x3544534F444"; String a = "0x3544534F444";
final boolean isHex = HexUtil.isHexNumber(a); Assert.assertTrue(HexUtil.isHexNumber(a));
Assert.assertTrue(isHex);
// https://gitee.com/dromara/hutool/issues/I62H7K
a = "0x0000000000000001158e460913d00000";
Assert.assertTrue(HexUtil.isHexNumber(a));
// 错误的
a = "0x0000001000T00001158e460913d00000";
Assert.assertFalse(HexUtil.isHexNumber(a));
} }
@Test @Test