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 224cbf12e..34e788f02 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -136,7 +136,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "MULTINOMIAL", null); r(m, "NETWORKDAYS", NetworkdaysFunction.instance); r(m, "NOMINAL", null); - r(m, "OCT2BIN", null); + r(m, "OCT2BIN", Oct2Dec.instance); r(m, "OCT2DEC", null); r(m, "OCT2HEX", null); r(m, "ODDFPRICE", null); diff --git a/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java b/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java new file mode 100644 index 000000000..110e869fb --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java @@ -0,0 +1,78 @@ +/* ==================================================================== + 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; + +/** + *
Some utils for converting from and to any base
+ * + * @author cedric dot walter @ gmail dot com + */ +public class BaseNumberUtils { + + + public static double convertToDecimal(String value, int base, int maxNumberOfPlaces) throws IllegalArgumentException { + if (value.isEmpty()) { + return 0.0; + } + + long stringLength = value.length(); + if (stringLength > maxNumberOfPlaces) { + throw new IllegalArgumentException(); + } + + double decimalValue = 0.0; + + long signedDigit = 0; + boolean hasSignedDigit = true; + char[] characters = value.toCharArray(); + for (char character : characters) { + long digit; + + if ('0' <= character && character <= '9') { + digit = character - '0'; + } else if ('A' <= character && character <= 'Z') { + digit = 10 + (character - 'A'); + } else if ('a' <= character && character <= 'z') { + digit = 10 + (character - 'a'); + } else { + digit = base; + } + + if (digit < base) { + if (hasSignedDigit) { + hasSignedDigit = false; + signedDigit = digit; + } + decimalValue = decimalValue * base + digit; + } else { + throw new IllegalArgumentException("character not allowed"); + } + } + + boolean isNegative = (!hasSignedDigit && stringLength == maxNumberOfPlaces && (signedDigit >= base / 2)); + if (isNegative) { + decimalValue = getTwoComplement(base, maxNumberOfPlaces, decimalValue); + decimalValue = decimalValue * -1.0; + } + + return decimalValue; + } + + private static double getTwoComplement(double base, double maxNumberOfPlaces, double decimalValue) { + return (Math.pow(base, maxNumberOfPlaces) - decimalValue); + } +} diff --git a/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java b/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java index 6fdebf52c..ccd641eae 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java +++ b/src/java/org/apache/poi/ss/formula/functions/Hex2Dec.java @@ -20,8 +20,6 @@ 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. * @@ -41,53 +39,16 @@ public class Hex2Dec extends Fixed1ArgFunction implements FreeRefFunction { public static final FreeRefFunction instance = new Hex2Dec(); + static final int HEXADECIMAL_BASE = 16; + static final int MAX_NUMBER_OF_PLACES = 10; + public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) { - String number = OperandResolver.coerceValueToString(numberVE); - if (number.length() > 10) { + String hex = OperandResolver.coerceValueToString(numberVE); + try { + return new NumberEval(BaseNumberUtils.convertToDecimal(hex, HEXADECIMAL_BASE, MAX_NUMBER_OF_PLACES)); + } catch (IllegalArgumentException e) { 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) { diff --git a/src/java/org/apache/poi/ss/formula/functions/Oct2Dec.java b/src/java/org/apache/poi/ss/formula/functions/Oct2Dec.java new file mode 100644 index 000000000..db9447df5 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/functions/Oct2Dec.java @@ -0,0 +1,64 @@ +/* ==================================================================== + 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.ErrorEval; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.ValueEval; + +/** + *Implementation for Excel Oct2Dec() function.
+ *+ * Converts an octal number to decimal. + *
+ *
+ * Syntax:
Oct2Dec (number )
+ *