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: Printing a Decimal Equivalent of a Binary Number

1. ## Printing a Decimal Equivalent of a Binary Number

This problem is from Java How to Program 9e, chapter 4, exercise 31.

It asks me to convert a binary number in to decimal.
How to program this in java? I am not able to find an effective algorithm to convert in to Java.

2. ## Re: Printing a Decimal Equivalent of a Binary Number

convert a binary number in to decimal.
Are you talking about Strings that contain numbers as characters?
For example: "0101" and "5"

The wrapper classes like Integer and Long have methods for converting a String to a number.

3. ## Re: Printing a Decimal Equivalent of a Binary Number

That's a problem. If I had to work with strings, I would have done it myself. We have to scan an integer value from CMD using nextInt(). The problem I am facing is that I am not able to find an algorithm to solve the problem.

4. ## Re: Printing a Decimal Equivalent of a Binary Number

If I had to work with strings, I would have done it myself.
Well, I think you're going to end up doing it yourself one way or the other, but if you could do it with a String object as an input, I don't see why you can't do it with an int value as an input.

Sometimes you have to think up or create an algorithm to solve a problem, and you'll be better for it.

Edit: The book pretty much gives you an algorithm: "[Hint: Use the remainder and division operators to pick off the binary number’s digits one at a time, from right to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]"

5. ## Re: Printing a Decimal Equivalent of a Binary Number

Originally Posted by Arslan Ahmad 656
We have to scan an integer value from CMD using nextInt().
First, Scanner has the useRadix(int radix) method. If you used this, the conversion binary-to-decimal would be a "breeze" and I guess this is not the goal of the exercise.
So if you input a number like 10100101 it "seems" a binary number but it really is a decimal number. If you want to think it as a binary .... ok (for the purpose of the exercise). Simply think that each decimal digit is just only 0...1.

Originally Posted by Arslan Ahmad 656
The problem I am facing is that I am not able to find an algorithm to solve the problem.
As I said in last phrase, you have decimal digits. Use division/modulo operators by 10 to get all decimal digits. A minimal validation is possible, just to check that the value is only 0 or 1. Then you have binary digits 0/1 and you can start multiplying by 2 (or power of 2, depending on the approach) and adding together to obtain the converted decimal value.

6. ## Re: Printing a Decimal Equivalent of a Binary Number

integer value from CMD using nextInt()
If the input to the problem is an int value in a variable, what do you want to do with the contents of that variable?
Given the int value: 123 what do you want to get as output?

7. ## Re: Printing a Decimal Equivalent of a Binary Number

Originally Posted by GregBrannon
Well, I think you're going to end up doing it yourself one way or the other, but if you could do it with a String object as an input, I don't see why you can't do it with an int value as an input.

Sometimes you have to think up or create an algorithm to solve a problem, and you'll be better for it.

"
What would I do if it were a string. I would simply multiply the characters by the increasing powers of two to get the equivalent decimal number.

--- Update ---

Originally Posted by Norm
Are you talking about Strings that contain numbers as characters?
For example: "0101" and "5"

The wrapper classes like Integer and Long have methods for converting a String to a number.
No, i am talking about integer input

--- Update ---

Originally Posted by GregBrannon
Well, I think you're going to end up doing it yourself one way or the other, but if you could do it with a String object as an input, I don't see why you can't do it with an int value as an input.

Sometimes you have to think up or create an algorithm to solve a problem, and you'll be better for it.

Edit: The book pretty much gives you an algorithm: "[Hint: Use the remainder and division operators to pick off the binary number’s digits one at a time, from right to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]"
Thanks Thanks Thanks!!!! A lot!!
So foolish of mine. I just didn't read the hint; it is really an algorithm in itself. Thanks for reminding me. Solved.