Converting Binary Numbers to Base 10 in Java - Program Help

  • Context: Java 
  • Thread starter Thread starter courtrigrad
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

The discussion focuses on writing a Java program to convert an array of binary numbers (0's and 1's) into base 10. Participants are seeking help with coding issues and logic related to the conversion process.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their initial code and expresses difficulty in obtaining the correct base 10 number.
  • Another participant suggests that the conversion should involve multiplying each binary digit by 2 raised to the power of its index in the array.
  • A question is raised about whether the return value should be the sum of the converted values instead of the final value of numValue.
  • A participant inquires how to adapt the program to handle a boolean array where true represents 1 and false represents 0, and asks about the use of Math.pow.
  • Another participant emphasizes the need to return the sum and suggests modifying the if statement for a boolean array. They also raise a question about the interpretation of the binary representation in the example provided.

Areas of Agreement / Disagreement

There is no consensus on the correct implementation of the program, as participants propose different approaches and raise questions about the logic and representation of binary numbers.

Contextual Notes

Participants have not resolved the specific implementation details, such as the handling of boolean arrays or the correct interpretation of binary representations.

courtrigrad
Messages
1,236
Reaction score
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
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.
 
Shouldn't you return sum
 
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
 
any ideas?
 
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!
 

Similar threads

Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K