0

this is the code I have written but it gives incorrect answers and I can't figure out why

int decimal = 0;

int power= 0;

for(int i=0; i<binary.length; i++)
{
    int tmp = binary[i]%10;
    decimal+=tmp*Math.pow(2,power);
    power++
}

System.out.println(decimal);
Emma Bell
  • 3
  • 1

1 Answers1

0

For arbitrary sized byte[] you can use BigInteger.

public void test(String[] args) {
    byte[] b = new byte[] {1,2,3,4,5};
    BigInteger bi = new BigInteger(b);
    System.out.println(bi.toString(10));
}

If your array is an int[] then you can convert it - see How to convert int[] to byte[].

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213