From 4e06f0261072ddc72b235427b0bd8d96c6aa865d Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 24 Nov 2022 13:33:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHexUtil.isHexNumber()?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91=E8=B6=85=E5=87=BAlong?= =?UTF-8?q?=E7=9A=84=E7=B2=BE=E5=BA=A6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../java/cn/hutool/core/util/HexUtil.java | 22 +++++++-------- .../java/cn/hutool/core/util/HexUtilTest.java | 27 ++++++++++++------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69f165da5..6e0d8a1cc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### 🐞Bug修复 * 【json 】 修复普通byte数组转JSONArray时的异常(pr#875@Gitee) * 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题(pr#874@Gitee) +* 【core 】 修复HexUtil.isHexNumber()判断逻辑超出long的精度问题(issue#I62H7K@Gitee) ------------------------------------------------------------------------------------------------------------- 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 a83f29e66..5a04f270e 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,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 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 0eed42588..161266c72 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 @@ -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); }