Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Binary to Decimal Conversion

1. ## 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?

2. ## Re: Binary to Decimal Conversion

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.

3. ## 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.

4. ## Re: Binary to Decimal Conversion

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.

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.

5. ## 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.