From 1e2e3c48f52b86e1b91cd7582805efd4c3657d48 Mon Sep 17 00:00:00 2001 From: qieugxi <2325661827@qq.com> Date: Thu, 25 Dec 2025 10:51:07 +0800 Subject: [PATCH] fix issue 4192 --- .../java/cn/hutool/core/util/HexUtil.java | 40 +++++++++++++++ .../java/cn/hutool/core/util/HexUtilTest.java | 49 +++++++++++++++++++ 2 files changed, 89 insertions(+) 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 1083e2897e..9ba459f6e4 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 @@ -329,6 +329,46 @@ public class HexUtil { return Long.parseLong(removeHexPrefix(value), 16); } + /** + * 转为16进制字符串 + * + * @param value float值 + * @return 16进制字符串 + */ + public static String toHex(float value) { + return Integer.toHexString(Float.floatToIntBits(value)); + } + + /** + * 16进制字符串转为float + * + * @param value 16进制字符串 + * @return 16进制字符串float值 + */ + public static float hexToFloat(String value) { + return Float.intBitsToFloat(Integer.parseUnsignedInt(removeHexPrefix(value), 16)); + } + + /** + * 转为16进制字符串 + * + * @param value double值 + * @return 16进制字符串 + */ + public static String toHex(double value) { + return Long.toHexString(Double.doubleToLongBits(value)); + } + + /** + * 16进制字符串转为double + * + * @param value 16进制字符串 + * @return 16进制字符串double值 + */ + public static double hexToDouble(String value) { + return Double.longBitsToDouble(Long.parseUnsignedLong(removeHexPrefix(value), 16)); + } + /** * 将byte值转为16进制并添加到{@link StringBuilder}中 * 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 f8e7a07454..00acaf1b30 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 @@ -107,6 +107,55 @@ public class HexUtilTest { assertEquals(255L, HexUtil.hexToLong(hex3)); } + @Test + public void hexToFloatTest() { + //测试正常浮点数值 + float value1 = 1.5f; + String hex1 = HexUtil.toHex(value1); + assertEquals(value1, HexUtil.hexToFloat(hex1)); + + //测试负数 + float value2 = -1.5f; + String hex2 = HexUtil.toHex(value2); + assertEquals(value2, HexUtil.hexToFloat(hex2)); + + //测试科学计数法值 + float value3 = 1.23456789e-5f; + String hex3 = HexUtil.toHex(value3); + assertEquals(value3, HexUtil.hexToFloat(hex3)); + + //测试十六进制前缀 + assertEquals(1.5f, HexUtil.hexToFloat("0x3fc00000")); + assertEquals(1.5f, HexUtil.hexToFloat("#3fc00000")); + } + + @Test + public void hexToDoubleTest() { + //测试正常双精度浮点数值 + double value1 = 1.5; + String hex1 = HexUtil.toHex(value1); + assertEquals(value1, HexUtil.hexToDouble(hex1)); + + //测试负数 + double value3 = -1.5; + String hex3 = HexUtil.toHex(value3); + assertEquals(value3, HexUtil.hexToDouble(hex3)); + + //测试高精度数值 + double value4 = Math.PI; + String hex4 = HexUtil.toHex(value4); + assertEquals(value4, HexUtil.hexToDouble(hex4)); + + //测试科学计数法值 + double value5 = 1.23456789012345e-10; + String hex5 = HexUtil.toHex(value5); + assertEquals(value5, HexUtil.hexToDouble(hex5)); + + //测试十六进制前缀 + assertEquals(1.5, HexUtil.hexToDouble("0x3ff8000000000000")); + assertEquals(1.5, HexUtil.hexToDouble("#3ff8000000000000")); + } + @Test public void toBigIntegerTest() { final String hex1 = "FF";