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