- #1
courtrigrad
- 1,236
- 2
Hello all:
I need to write a program that takes an array of 0's and 1's and converts them into base 10 numbers. Here is my code, but I can't get it to return the correct base 10 number.
Any help is appreciated.
Thanks!
I need to write a program that takes an array of 0's and 1's and converts them into base 10 numbers. Here is my code, but I can't get it to return the correct base 10 number.
Code:
/* This program returns a value in base 10 from an array of binary numbers.
*
*/
public class binary
{
public int convertBaseTen( int [] a) // method takes an array
{
int numValue=1;
int sum=0;
for(int i = 0; i< a.length; i++) // loop through array a
{
if (a[i]==1)
{
sum+=numValue;
}
numValue*=2;
}
return numValue;
}
}
Any help is appreciated.
Thanks!