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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Printing a Decimal Equivalent of a Binary Number

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    Lahore, Pakistan
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Help me, please.
    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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"

    If not, please explain.

    The wrapper classes like Integer and Long have methods for converting a String to a number.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    Lahore, Pakistan
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Printing a Decimal Equivalent of a Binary Number

    Quote Originally Posted by Arslan Ahmad 656 View Post
    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.

    Quote Originally Posted by Arslan Ahmad 656 View Post
    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.
    Last edited by andbin; February 28th, 2014 at 04:34 PM.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Location
    Lahore, Pakistan
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Printing a Decimal Equivalent of a Binary Number

    Quote Originally Posted by GregBrannon View Post
    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 ---

    Quote Originally Posted by Norm View Post
    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 ---

    Quote Originally Posted by GregBrannon View Post
    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.

Similar Threads

  1. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  2. Binary to Decimal Conversion
    By Java_boy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2012, 03:11 AM
  3. Decimal To binary Recursive conversion java?
    By sirdonclemo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:33 AM
  4. Decimal to binary conversion
    By baueml01 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2011, 06:42 AM
  5. [SOLVED] decimal to binary problem
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 26th, 2010, 07:48 PM

Tags for this Thread