From a0cba2fd55a42ce899084871a456f7d5ca55c68b Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 15 Jan 2023 11:01:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHexUtil.isHexNumber()?= =?UTF-8?q?=E5=AF=B9"-"=E7=9A=84=E5=88=A4=E6=96=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java | 6 +++++- .../src/test/java/cn/hutool/core/util/HexUtilTest.java | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0c658db7..ccb2e82b2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,11 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.12.M1 (2022-12-27) +# 5.8.12.M1 (2023-01-15) ### 🐣新特性 ### 🐞Bug修复 +* 【core 】 修复HexUtil.isHexNumber()对"-"的判断问题(issue#2857@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java index 5a04f270e..8d9dda219 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java @@ -27,7 +27,11 @@ public class HexUtil { * @return 是否为16进制 */ public static boolean isHexNumber(String value) { - int index = (value.startsWith("-") ? 1 : 0); + if(StrUtil.startWith(value, '-')){ + // issue#2875 + return false; + } + int index = 0; if (value.startsWith("0x", index) || value.startsWith("0X", index)) { index += 2; } else if (value.startsWith("#", index)) { diff --git a/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java index 161266c72..095381842 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java @@ -49,6 +49,10 @@ public class HexUtilTest { // 错误的 a = "0x0000001000T00001158e460913d00000"; Assert.assertFalse(HexUtil.isHexNumber(a)); + + // 错误的,https://github.com/dromara/hutool/issues/2857 + a = "-1"; + Assert.assertFalse(HexUtil.isHexNumber(a)); } @Test