diff --git a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index a3898a701..224cbf12e 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -89,7 +89,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "CUMIPMT", null); r(m, "CUMPRINC", null); r(m, "DEC2BIN", Dec2Bin.instance); - r(m, "DEC2HEX", null); + r(m, "DEC2HEX", Dec2Hex.instance); r(m, "DEC2OCT", null); r(m, "DELTA", Delta.instance); r(m, "DISC", null); 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 c4d36214e..62462b029 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java @@ -45,7 +45,7 @@ import org.apache.poi.ss.formula.eval.*; * @@ -54,9 +54,11 @@ import org.apache.poi.ss.formula.eval.*; */ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction { - private final static long MIN_VALUE = new Long("-549755813888").longValue(); - private final static long MAX_VALUE = new Long("549755813887").longValue(); - private final static int DEFAULT_PLACES_VALUE = 10;; + public static final FreeRefFunction instance = new Dec2Hex(); + + private final static long MIN_VALUE = Long.parseLong("-549755813888"); + private final static long MAX_VALUE = Long.parseLong("549755813887"); + private final static int DEFAULT_PLACES_VALUE = 10; public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) { ValueEval veText1; @@ -81,7 +83,8 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction int placesNumber = 0; if (number1 < 0) { placesNumber = DEFAULT_PLACES_VALUE; - } else { + } + else if (places != null) { ValueEval placesValueEval; try { placesValueEval = OperandResolver.getSingleValue(places, srcRowIndex, srcColumnIndex); @@ -104,26 +107,32 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction } } - String hex = ""; + String hex; if (placesNumber != 0) { - hex = String.format("%0"+placesNumber+"X", number1.intValue() & 0x0FFFFF); + hex = String.format("%0"+placesNumber+"X", number1.intValue()); } else { hex = Integer.toHexString(number1.intValue()); } + if (number1 < 0) { + hex = "FF"+ hex.substring(2); + } + return new StringEval(hex.toUpperCase()); } public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { - return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE))); + return this.evaluate(srcRowIndex, srcColumnIndex, arg0, null); } public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { - if (args.length != 2) { - return ErrorEval.VALUE_INVALID; + if (args.length == 1) { + return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]); } - return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); + if (args.length == 2) { + return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); + } + return ErrorEval.VALUE_INVALID; } - -} \ No newline at end of file +} diff --git a/test-data/spreadsheet/FormulaEvalTestData.xls b/test-data/spreadsheet/FormulaEvalTestData.xls index 0414f9eac..4245acb88 100644 Binary files a/test-data/spreadsheet/FormulaEvalTestData.xls and b/test-data/spreadsheet/FormulaEvalTestData.xls differ