# Binary to Decimal Conversion

Printable View

• January 6th, 2012, 06:05 PM
Java_boy
Binary to Decimal Conversion
Hey everyone, as I am totally new to Java programming, I am currently trying to convert a Binary number to Decimal. The task was to :

Use a loop to read (charAt()) each digit (0/1 char) in the input string, scanning from right to left;
Use the loop to build the required powers of 2;
Use a conditional statement to deal with 0 and 1 separately;
Debug using simple input, e.g. 1, 10, 101, and print intermediate values in the loop.

Use your program to find the decimal value of the following binary number:
1001010101011010111001011101010101010101

public class BinarytoDecimal {
public static void main (String[]args) {

String digit = "1001010101011010111001011101010101010101";
String output = "";
for (int i= 0; i <digit.length(); i++) { // walk over all digits
output*= 2; // prepare for another digit, see above
output+= // digit at location i

System.out.println(output);
}
}

And with my very beginner knowledge of Java, I wrote the above code which still does not work. I would be delighted if anyone can help me with fixing the code? :)
• January 6th, 2012, 06:32 PM
Norm
Re: Binary to Decimal Conversion
Quote:

I wrote the above code which still does not work.
Please explain. Show what the code is doing and explain what it should have done.
Try testing with a smaller String of binary digits before attempting to solve the long ones.
• January 6th, 2012, 08:23 PM
Java_boy
Re: Binary to Decimal Conversion
Well I gave it a go and when it is compiled the message given is "BUILD SUCCESSFUL (total time: 0 seconds)"
The two errors are: (I have written in the syntax)

public class BinarytoDecimal {
public static void main (String[]args) {

String digit = "1001010101011010111001011101010101010101";
String output = "";
for (int i= 0; i <digit.length(); i++) { // walk over all digits
output*= 2; // error operator * cannot be applied to java.lang.String, int
output+= // error operator + cannot be applied to java.lang.String, int

System.out.println(output);
}
}

Basically, I have two Strings, One which is String digit (which will be the binary number), other String output (which prepares an empty output initially). In order for me to convert my String digit into binary, I need to use a for loop which will build required powers of 2 and conditional statement which deals with 1 and 0 separately and using the loop to read (charAt()) each digit (0/1 char)( which the part I don't understand how to do) so i gave it a shot anyway.
• January 6th, 2012, 08:30 PM
pbrockway2
Re: Binary to Decimal Conversion
Quote:

output*= 2; // error operator * cannot be applied to java.lang.String, int
The error message means what it says ;) You can't multiply a string like output because string multiplication doesn't make sense. Consider making output an int so you can multiply it. You can always make a string from it later if you want to.

Quote:

output+= // error operator + cannot be applied to java.lang.String, int
This line isn't finished. You have to add something to output. The chances are you will use an if statement and check what character you are looking at in the digit string. Depending on its value you will or will not add something to output.
• January 7th, 2012, 02:11 AM
elisha.java
Re: Binary to Decimal Conversion
This is my opinion on this question: Do you know how to convert base ten numbers into binary format? If you answered yes to this question, then I think what you need to do is to reverse engineer the code you use in your conversion. If you answered No, then you might want to go back to the drawing board! Check out a tutorial or visit binary.com for tips on how to do such a conversion.