Index: poi/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- poi/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java (revision )
+++ poi/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java (revision )
@@ -0,0 +1,87 @@
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.OperandResolver;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+
+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();
+
+ @Override
+ 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");
+ }
+
+ String result = "";
+ if (isPositive) {
+ try {
+ result = String.valueOf(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);
+ result = String.valueOf(temp.longValue());
+ } else {
+ try {
+ result = String.valueOf(Integer.parseInt(unsigned, 16));
+ } catch (NumberFormatException ee) {
+ // number is not a valid hexadecimal number
+ return ErrorEval.NUM_ERROR;
+ }
+ }
+ }
+
+ return new StringEval(result);
+ }
+
+ @Override
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
+ }
+}
Index: poi/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- poi/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (revision 1488867)
+++ poi/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (revision )
@@ -108,7 +108,7 @@
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);
Index: poi/src/testcases/org/apache/poi/ss/formula/functions/TestHex2DecFunctionsFromSpreadsheet.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- poi/src/testcases/org/apache/poi/ss/formula/functions/TestHex2DecFunctionsFromSpreadsheet.java (revision )
+++ poi/src/testcases/org/apache/poi/ss/formula/functions/TestHex2DecFunctionsFromSpreadsheet.java (revision )
@@ -0,0 +1,31 @@
+/* ====================================================================
+ 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;
+
+/**
+ * Tests Hex2Dec() as loaded from a test data spreadsheet.
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public class TestHex2DecFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {
+
+ @Override
+ protected String getFilename() {
+ return "Hex2DecFunctionTestCaseData.xls";
+ }
+}
\ No newline at end of file