Converting Binary Numbers to Base 10 in Java - Program Help

  • Java
  • Thread starter courtrigrad
  • Start date
  • Tags
    Java Program
In summary, the conversation discussed the creation of a program that converts an array of binary numbers into base 10 numbers. Suggestions were given to use a loop and the power of 2 to calculate the base 10 value, and to return the sum instead of the numValue. It was also mentioned to start the loop from the last element in the array to correctly calculate the base 10 value.
  • #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.

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!
 
Technology news on Phys.org
  • #2
dont just multiply the number in the array by 2, multiply it by 2^i
i being the integer varibale you are using in the loop to go through the array, because with eah successive element in the array, you are increasing by a power of 2.

for example if you have a number of let's say...5
5 = 101 in binary

101 = (1*2^0) + (0*2^4) + (1*2^2) = 5

if you loop through the array from lowest power of 2 to highest power of 2, and multiply the element by 2^i, and then keep a running total of all that, then you should get the correct answer.
 
  • #3
Shouldn't you return sum
 
  • #4
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;
}
    }

How would I write this program which takes in a boolean where true = 1, and false = 0?

Example:

{true, true, false, true} = [1,1,0,1] and then convert this into base 10? Should I use math.pow?

Thanks
 
  • #5
any ideas?
 
  • #6
1st, return sum, not numValue.

Also, for a boolean array, your if statement would be if(a).

Another thing, is {1,1,0,1} representing 1101 or 1011? (Binary). If it's the second option, changing the return value to sum should be all that needs fixing. If not, you should start at a.length - 1 and go down to (and including) 0 at the array. Then it will calculate the last value and go to the first. In {1,1,0,1} would start at the last 1.

Hope that helped!
 

1. How do I convert a binary number to base 10 in Java?

To convert a binary number to base 10 in Java, you can use the Integer.parseInt() method. First, pass in the binary number as a String, followed by the base 2 as the second argument. This will return an integer value in base 10.

2. Can you provide an example of converting a binary number to base 10 in Java?

Sure! Let's say we have the binary number "101010" and we want to convert it to base 10. We can use the Integer.parseInt() method like this: int decimal = Integer.parseInt("101010", 2); This will return the decimal value of 42.

3. What happens if the binary number contains letters or symbols?

If the binary number contains letters or symbols, the Integer.parseInt() method will throw a NumberFormatException. This is because it can only convert numbers in base 2 to base 10.

4. Is there a limit to the size of the binary number that can be converted to base 10?

Yes, there is a limit to the size of the binary number that can be converted. In Java, the largest integer value is 2,147,483,647. If the binary number is larger than this, the conversion may result in an overflow.

5. Are there any other methods for converting binary numbers to base 10 in Java?

Yes, there are other methods for converting binary numbers to base 10 in Java, such as using loops or bitwise operations. However, the Integer.parseInt() method is the most commonly used and efficient method for conversion.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
891
  • Programming and Computer Science
Replies
1
Views
987
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
632
Back
Top