diff options
author | Cédric Walter <cedricwalter@apache.org> | 2013-10-11 19:06:33 +0000 |
---|---|---|
committer | Cédric Walter <cedricwalter@apache.org> | 2013-10-11 19:06:33 +0000 |
commit | 7809ede62ecae6e8f35abc9a8d06b5d27b5e06a1 (patch) | |
tree | 1e3e6ff34aad39c4987d53525bc960e090252c7c | |
parent | f7f52a98e50b20337028f09109c60c2167cd6db7 (diff) | |
download | poi-7809ede62ecae6e8f35abc9a8d06b5d27b5e06a1.tar.gz poi-7809ede62ecae6e8f35abc9a8d06b5d27b5e06a1.zip |
Bug 55057: patch for missing function Hex2Dec
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1531393 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java | 2 | ||||
-rw-r--r-- | src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java | 99 | ||||
-rw-r--r-- | test-data/spreadsheet/FormulaEvalTestData.xls | bin | 172032 -> 172544 bytes |
3 files changed, 100 insertions, 1 deletions
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.<p/>
+ * <p/>
+ * <b>Syntax</b>:<br/> <b>HEX2DEC </b>(<b>number</b>)<br/>
+ * <p/>
+ * Converts a hexadecimal number to decimal.
+ * <p/>
+ * 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 Binary files differindex f7de0a1d77..0414f9eac9 100644 --- a/test-data/spreadsheet/FormulaEvalTestData.xls +++ b/test-data/spreadsheet/FormulaEvalTestData.xls |