From 117e591778b92fe92e6f19b0e5e7b81d8000f6c4 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 9 Jan 2015 20:37:11 +0000 Subject: Adjust Bin2Dec similar to what we did for Hex2Dec in Bug 57196 and fix some smaller problems in the implementation found while adding full unit tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1650654 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/formula/functions/Bin2Dec.java | 44 +++++++++++++--------- .../apache/poi/ss/formula/functions/Dec2Bin.java | 4 +- 2 files changed, 29 insertions(+), 19 deletions(-) (limited to 'src/java') diff --git a/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java b/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java index added40cc0..9617877ca6 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java +++ b/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java @@ -20,6 +20,7 @@ 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.RefEval; import org.apache.poi.ss.formula.eval.ValueEval; /** @@ -44,7 +45,13 @@ public class Bin2Dec extends Fixed1ArgFunction implements FreeRefFunction { public static final FreeRefFunction instance = new Bin2Dec(); public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) { - String number = OperandResolver.coerceValueToString(numberVE); + final String number; + if (numberVE instanceof RefEval) { + RefEval re = (RefEval) numberVE; + number = OperandResolver.coerceValueToString(re.getInnerValueEval(re.getFirstSheetIndex())); + } else { + number = OperandResolver.coerceValueToString(numberVE); + } if (number.length() > 10) { return ErrorEval.NUM_ERROR; } @@ -62,22 +69,25 @@ public class Bin2Dec extends Fixed1ArgFunction implements FreeRefFunction { } String value; - int sum; - if (isPositive) { - //bit9*2^8 + bit8*2^7 + bit7*2^6 + bit6*2^5 + bit5*2^4+ bit3*2^2+ bit2*2^1+ bit1*2^0 - sum = getDecimalValue(unsigned); - value = String.valueOf(sum); - } else { - //The leftmost bit is 1 -- this is negative number - //Inverse bits [1-9] - String inverted = toggleBits(unsigned); - // Calculate decimal number - sum = getDecimalValue(inverted); - - //Add 1 to obtained number - sum++; - - value = "-" + String.valueOf(sum); + try { + if (isPositive) { + //bit9*2^8 + bit8*2^7 + bit7*2^6 + bit6*2^5 + bit5*2^4+ bit3*2^2+ bit2*2^1+ bit1*2^0 + int sum = getDecimalValue(unsigned); + value = String.valueOf(sum); + } else { + //The leftmost bit is 1 -- this is negative number + //Inverse bits [1-9] + String inverted = toggleBits(unsigned); + // Calculate decimal number + int sum = getDecimalValue(inverted); + + //Add 1 to obtained number + sum++; + + value = "-" + String.valueOf(sum); + } + } catch (NumberFormatException e) { + return ErrorEval.NUM_ERROR; } return new NumberEval(Long.parseLong(value)); diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java index 814b3eb2c6..21d8bcfaf0 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java @@ -52,8 +52,8 @@ public class Dec2Bin extends Var1or2ArgFunction implements FreeRefFunction { public static final FreeRefFunction instance = new Dec2Bin(); - private final static long MIN_VALUE = Long.parseLong("-512"); - private final static long MAX_VALUE = Long.parseLong("512"); + private final static long MIN_VALUE = -512; + private final static long MAX_VALUE = 511; private final static int DEFAULT_PLACES_VALUE = 10; public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval placesVE) { -- cgit v1.2.3