You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2013/10/11 21:06:34 UTC

svn commit: r1531393 - in /poi/trunk: src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java test-data/spreadsheet/FormulaEvalTestData.xls

Author: cedricwalter
Date: Fri Oct 11 19:06:33 2013
New Revision: 1531393

URL: http://svn.apache.org/r1531393
Log:
Bug 55057: patch for missing function Hex2Dec

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
    poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java?rev=1531393&r1=1531392&r2=1531393&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java Fri Oct 11 19:06:33 2013
@@ -106,7 +106,7 @@ public final class AnalysisToolPak imple
         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);

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java?rev=1531393&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java Fri Oct 11 19:06:33 2013
@@ -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]);
+    }
+}

Modified: poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/FormulaEvalTestData.xls?rev=1531393&r1=1531392&r2=1531393&view=diff
==============================================================================
Binary files - no diff available.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org