3

I have a BigInteger method which takes a string[] array input of 4 numbers, converts the numbers into an int[], and then applies numerous mathematical operations to it.

public BigInteger convert32Bit(String[] array)
{
    System.out.println("Array being converted is "+Arrays.toString(array)+"\n");
    int[] tempArray = new int[array.length];
    ArrayList<BigInteger> tempBigIntList = new ArrayList<BigInteger>();
    int i = 0;
    for(String s:array)
    {
        int power = 4-i;
        tempArray[i]= Integer.parseInt(s);
        String string = Integer.toString(tempArray[0]);
        BigInteger myBigInt = new BigInteger(string);
        BigInteger num2 = myBigInt.multiply(new BigInteger("256").pow(power));
        System.out.println(tempArray[i]+" is being multiplied by 256^"+power+" which equals "+num2);
        tempBigIntList.add(num2);
        i++;
    }

    BigInteger bigInt32Bit = new BigInteger("0");
    for(BigInteger bI:tempBigIntList)
    {
        bigInt32Bit.add(bI);
    }

    System.out.println("\nThe final value is "+bigInt32Bit);

    return bigInt32Bit;
}

However there is a problem. If I take the array "123", "0", "245", "23" as the input. I get the following output.

Wrong output

The output I am expecting is

Array being converted is [123, 0, 245, 23]

123 is being multiplied by 256^4 which equals 528280977408
0 is being multiplied by 256^3 which equals 0
245 is being multiplied by 256^2 which equals 16056320
23 is being multiplied by 256^1 which equals 5888

The final value is 528297039616

Can someone please help fix this?

Dan
  • 7,286
  • 6
  • 49
  • 114

2 Answers2

3

Replace this line

bigInt32Bit.add(bI);

with

bigInt32Bit = bigInt32Bit.add(bI);

You do this because BigInteger is immutable. This means that you have to create a new value for bigInt32Bit instead of just adjusting an old one. Also (as @justhalf says) replace the line

String string = Integer.toString(tempArray[0]);

with

String string = Integer.toString(tempArray[i]);

so that you use the correct value when applying the mathematical operators.

Community
  • 1
  • 1
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • 1
    Also there is a typo in his code, where `String string = Integer.toString(tempArray[0]);` should be `tempArray[i]` instead of `tempArray[0]` – justhalf Dec 23 '15 at 13:51
  • Thank you both of you. Fixed the problems :) – Dan Dec 23 '15 at 13:52
  • 1
    @TagirValeev: Actually you don't need to change it to community wiki, it's just a small typo =) – justhalf Dec 23 '15 at 13:52
0

BigInteger is immutable and hence bigInt32Bit.add(bI); would lead the value of whatever you have first element. Inorder to add all the values you could do something like:

 bigInt32Bit = bigInt32Bit.add(bI);//assign it

Also you are just passing very first element of your array as input to bigInteger like String string = Integer.toString(tempArray[0]);, it should be String string = Integer.toString(tempArray[i]);. I would not use array if its not used anywhere insteadi would just use an integer variable.

SMA
  • 36,381
  • 8
  • 49
  • 73