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 bf9f8b743b..a3898a7018 100644
--- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
+++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
@@ -106,7 +106,7 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "GCD", null);
r(m, "GESTEP", null);
r(m, "HEX2BIN", null);
- r(m, "HEX2DEC", null);
+ r(m, "HEX2DEC", Hex2Dec.instance);
r(m, "HEX2OCT", null);
r(m, "IFERROR", IfError.instance);
r(m, "IMABS", null);
diff --git a/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java b/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
new file mode 100644
index 0000000000..6fdebf52c2
--- /dev/null
+++ b/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
@@ -0,0 +1,99 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.*;
+
+import java.math.BigInteger;
+
+/**
+ * Implementation for Excel HEX2DEC() function.
+ *
+ * Syntax:
HEX2DEC (number)
+ *
+ * Converts a hexadecimal number to decimal.
+ *
+ * Number is the hexadecimal number you want to convert. Number cannot contain more than 10 characters (40 bits).
+ * The most significant bit of number is the sign bit.
+ * The remaining 39 bits are magnitude bits. Negative numbers are represented using two's-complement notation.
+ * Remark
+ * If number is not a valid hexadecimal number, HEX2DEC returns the #NUM! error value.
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class Hex2Dec extends Fixed1ArgFunction implements FreeRefFunction {
+
+ public static final FreeRefFunction instance = new Hex2Dec();
+
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
+ String number = OperandResolver.coerceValueToString(numberVE);
+ if (number.length() > 10) {
+ return ErrorEval.NUM_ERROR;
+ }
+
+ String unsigned;
+ boolean isPositive = false;
+ boolean isNegative = false;
+ if (number.length() < 10) {
+ unsigned = number;
+ isPositive = true;
+ } else {
+ //remove sign bit
+ unsigned = number.substring(1);
+ isNegative =
+ number.startsWith("8") || number.startsWith("9") ||
+ number.startsWith("A") || number.startsWith("B") ||
+ number.startsWith("C") || number.startsWith("D") ||
+ number.startsWith("E") || number.startsWith("F");
+ }
+
+ long decimal;
+ if (isPositive) {
+ try {
+ decimal = Integer.parseInt(unsigned, 16);
+ } catch (NumberFormatException ee) {
+ // number is not a valid hexadecimal number
+ return ErrorEval.NUM_ERROR;
+ }
+ } else {
+ if (isNegative) {
+ BigInteger temp = new BigInteger(unsigned, 16);
+ BigInteger subtract = BigInteger.ONE.shiftLeft(unsigned.length() * 4);
+ temp = temp.subtract(subtract);
+ decimal = temp.longValue();
+ } else {
+ try {
+ decimal = Integer.parseInt(unsigned, 16);
+ } catch (NumberFormatException ee) {
+ // number is not a valid hexadecimal number
+ return ErrorEval.NUM_ERROR;
+ }
+ }
+ }
+
+ return new NumberEval(decimal);
+ }
+
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
+ }
+}
diff --git a/test-data/spreadsheet/FormulaEvalTestData.xls b/test-data/spreadsheet/FormulaEvalTestData.xls
index f7de0a1d77..0414f9eac9 100644
Binary files a/test-data/spreadsheet/FormulaEvalTestData.xls and b/test-data/spreadsheet/FormulaEvalTestData.xls differ