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"
If not, please explain.
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.