+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: decimal to binary number

  1. #1
    AstralX's Avatar
    AstralX is offline Newbie
    Join Date
    Feb 2010
    Location
    Slovenia
    Posts
    7
    Rep Power
    0

    decimal to binary number

    Ok. this is my first tutorial so i hope that you will understand.
    How this programme works:
    1. Input - decimal number
    2. output - binary number

    So here is code:

    Code:
    import java.util.*;
    
    public class number
    {
    	public static void main (String [] args)
    	{
    		Scanner input = new Scanner (System.in);
    		System.out.println ("Input decimal number");
    		int decimal = input.nextInt ();
    		input.close ();
    		
    		int base = 2;
    		int result = 0;
    		int multiplier = 1;
    		
    		while (decimal>0)
    		{
    			int residue = decimal%base;
    			decimal = decimal/base;
    			result = result +residue*multiplier;
    			multiplier = multiplier * 10;
    		}
    		System.out.println ("binary....."+result);
    	}
    }
    When i try it it was working although there can be some mistake so try it.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: decimal to binary number

    Input decimal number
    1000000
    binary.....1253788736


    Nope, doesn't work.

  4. #3
    curse is offline Newbie
    Join Date
    Mar 2010
    Location
    Ottawa, Canada
    Posts
    21
    Rep Power
    0

    Re: decimal to binary number

    Just whipped this up in a few minutes. It seems to work, although it could probably be re-factored.

    EDIT: Re-factored a bit.
    Code:
    public int BinaryToDecimal(int bin)
    {
        /* Base cases */
        if (bin == 0) return 0;
        if (bin == 1) return 1;
    		
        /* Store the binary number as a String. */
        String number = Integer.toString(bin);
    
        /* Most- and least-significant digits */
        int MSD = (int)(java.lang.Math.pow(2, number.length()-1));
        int LSDs = Integer.parseInt(number.substring(1));
    	
        /* Recursion */
        return MSD + BinaryToDecimal(LSDs);
    }
    Unit tests:
    0 to decimal is: 0
    1 to decimal is: 1
    1111 to decimal is: 15
    1000000 to decimal is: 64
    11110000 to decimal is: 240


    I realize that this method is not the most efficient method, however I think it's a good demonstration of how and when recursion can be used.

    @OP:
    • You should unit-test your program before saying that it works. Trying 1 or 2 similar cases is not enough. You need to try to BREAK your code.
    • Never put computations in the main() method... That completely contradicts the reason Java exists.
    Last edited by curse; 03-04-2010 at 04:15 PM.

  5. #4
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Re: decimal to binary number

    I tried to do this exact same thing a few nights ago! I'm a bit older, & pretty useless at even pretty simple math problems sometimes, so I ran into a problem...

    Here's my question: how big should an array be to store a decimal number as binary? I kept thinking it's 2n or something, but nahhh obviously not.
    Here's the method I came up with to solve the problem (the array would have to be printed backwards to get the right answer, i realize that much):

    Code:
     
    public void convert(int number) {
    
       int[] binaryArray = new int[?WHAT_SIZE?];
       int i = 0;
    
       while (number > 0) {
          
          binaryArray[i] = number % 2;
          number = number / 2;
          i++;
    
       } 
    }
    Last edited by unscripted_1; 04-21-2010 at 10:25 PM. Reason: duhhhh

  6. #5
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: decimal to binary number

    That's just awful, why would you want to store the binary string in an int array? Much better way would be to use a String.

    Every integer is 4 bytes long, your binary digit could take take 4 times less space and be stored in a byte.
    Ideally, it would only need a bit to store one binary digit, but processors don't manipulate single bits, and this would require additional operations to display.


    there's already a method for that, no need to figure that out yourself:

    Code:
    Integer.toString(decimal, 2);
    or

    Code:
    Integer.toBinaryString(decimal);
    Last edited by Sinipull; 04-22-2010 at 06:55 AM.

  7. #6
    curse is offline Newbie
    Join Date
    Mar 2010
    Location
    Ottawa, Canada
    Posts
    21
    Rep Power
    0

    Re: decimal to binary number

    Quote Originally Posted by unscripted_1 View Post
    I tried to do this exact same thing a few nights ago! I'm a bit older, & pretty useless at even pretty simple math problems sometimes, so I ran into a problem...

    Here's my question: how big should an array be to store a decimal number as binary? I kept thinking it's 2n or something, but nahhh obviously not.
    Here's the method I came up with to solve the problem (the array would have to be printed backwards to get the right answer, i realize that much):

    Code:
     
    public void convert(int number) {
    
       int[] binaryArray = new int[?WHAT_SIZE?];
       int i = 0;
    
       while (number > 0) {
          
          binaryArray[i] = number % 2;
          number = number / 2;
          i++;
    
       } 
    }
    Just look up some basic binary conversion algorithm... Understand how to get to and from binary on paper, then write out the logic.

  8. #7
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Re: decimal to binary number

    yea I came to that conclusion myself while I was thinking about it at work tonight. Sigh, but of course there's already a function that returns your int as a binary String. Everything's already been written by someone at some time

  9. #8
    curse is offline Newbie
    Join Date
    Mar 2010
    Location
    Ottawa, Canada
    Posts
    21
    Rep Power
    0

    Re: decimal to binary number

    Quote Originally Posted by unscripted_1 View Post
    yea I came to that conclusion myself while I was thinking about it at work tonight. Sigh, but of course there's already a function that returns your int as a binary String. Everything's already been written by someone at some time
    Of course. However, one of my software engineering professors really liked to give us assignments where we would code stuff that was pretty commonplace in the programming world. It was good practice to become familiar with the language and how things actually work.

    Besides, if you ever work professionally with code, 90-95% of all your project's code is probably copy-pasted from somewhere else. Like the code for a TCP connection... No one really bothers to memorize it, they just pull it up and copy it over.

  10. #9
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Re: decimal to binary number

    My professor was the same, he liked us to write our own versions of already existing methods & functions, as it's awesome practice.
    re: Storing a binary number as an int array... I know it's pretty ghastly at first sight. I was writing a wierd little program late at night when I came up with it. The idea behind it is 'what if you want to actually do something different with the binary? Rather than just print it?'. It seemed easier to me to work with it as an array for some reason...

  11. #10
    curse is offline Newbie
    Join Date
    Mar 2010
    Location
    Ottawa, Canada
    Posts
    21
    Rep Power
    0

    Re: decimal to binary number

    Quote Originally Posted by unscripted_1 View Post
    My professor was the same, he liked us to write our own versions of already existing methods & functions, as it's awesome practice.
    re: Storing a binary number as an int array... I know it's pretty ghastly at first sight. I was writing a wierd little program late at night when I came up with it. The idea behind it is 'what if you want to actually do something different with the binary? Rather than just print it?'. It seemed easier to me to work with it as an array for some reason...
    It might be easier for you to work with arrays but it's rather costly in terms of resources. If you know the proper method calls for the String class, it's just as easy to do the same thing with a String.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Decimal To Binary
    By neshkid123 in forum Java Help
    Replies: 15
    Last Post: 10-13-2011, 03:44 PM
  2. Convert Binary Integer to Decimal
    By Kinsleyy in forum Java Help
    Replies: 10
    Last Post: 02-12-2010, 12:25 PM
  3. Replies: 3
    Last Post: 01-12-2010, 08:20 AM
  4. Binary to Decimal and back
    By atomicSpatule in forum C# Programming
    Replies: 2
    Last Post: 03-28-2007, 09:43 AM
  5. Binary, Decimal, Hex, the Manual way!!
    By TcM in forum Tutorials
    Replies: 22
    Last Post: 01-09-2007, 10:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts