From e47a129a5bb6a4ef21c44b82668bb981a7a1afac Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 17 Sep 2015 19:23:25 +0000 Subject: [PATCH] Bug 57915: Fix Dev2Hex for numbers larger than Integer.MAX_VALUE and less than Integer.MIN_VALUE git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1703672 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/Dec2Hex.java | 2 +- .../poi/ss/formula/functions/TestDec2Hex.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java index 54a624652d..6318f98460 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java @@ -112,7 +112,7 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction hex = String.format(Locale.ROOT, "%0"+placesNumber+"X", number1.intValue()); } else { - hex = Integer.toHexString(number1.intValue()); + hex = Long.toHexString(number1.longValue()); } if (number1 < 0) { diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java b/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java index f860aff2a5..e9b6c44e6a 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java @@ -81,6 +81,28 @@ public final class TestDec2Hex extends TestCase { confirmValue("Converts decimal -54 to hexadecimal, 2 is ignored","-54", "2", "FFFFFFFFCA"); confirmValue("places is optionnal","-54", "FFFFFFFFCA"); + + confirmValue("Converts normal decimal number to hexadecimal", "100", "64"); + + String maxInt = Integer.toString(Integer.MAX_VALUE); + assertEquals("2147483647", maxInt); + confirmValue("Converts INT_MAX to hexadecimal", maxInt, "7FFFFFFF"); + + String minInt = Integer.toString(Integer.MIN_VALUE); + assertEquals("-2147483648", minInt); + confirmValue("Converts INT_MIN to hexadecimal", minInt, "FF80000000"); + + String maxIntPlusOne = Long.toString(((long)Integer.MAX_VALUE)+1); + assertEquals("2147483648", maxIntPlusOne); + confirmValue("Converts INT_MAX + 1 to hexadecimal", maxIntPlusOne, "80000000"); + + String maxLong = Long.toString(549755813887l); + assertEquals("549755813887", maxLong); + confirmValue("Converts the max supported value to hexadecimal", maxLong, "7FFFFFFFFF"); + + String minLong = Long.toString(-549755813888l); + assertEquals("-549755813888", minLong); + confirmValue("Converts the min supported value to hexadecimal", minLong, "FF80000000"); } public void testErrors() {