Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Convert Binary Integer to Decimal

  1. #1
    Kinsleyy is offline Newbie
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Convert Binary Integer to Decimal

    Hi I am trying to write an app that inputs an integer containing only 0s and 1s (a binary integer) and prints its decimal equivalent. I am aware that there are some very short ways to do this but as a beginner I am writing this to show the process. I am trying to use the remainder and division operators to complete this. Here is what I have, is there a different route I can take? Any help will be greatly appreciated! thanks




    Code:
    import java.util.Scanner;
    
    public class BinarytoDecimal {
    
        
            
            
            public static void main(String[] args) {
           
                    int r;
                    int s = 0;
                    int p = 1;
                    
    
                 Scanner in = new Scanner(System.in);
                 System.out.println("Binary to Decimal");
                 System.out.println("Enter a Binary Number which is to be converted");//ask to input the number which is to be converted
                 int n= in.nextInt();
                 
    
                 
    
                 
    
    
                 while(n > 0)
                 {
                     r = n % 10;
                     n = n / 10;
                     if(r!= 0 && r!= 1)
                     {
                         System.out.println("This is not a binary number.!!!");
                         System.out.println("Please try once again.!!!");
                         System.exit(0);//exits if the number is non binary
                     }
                 }
                
                 System.out.print("Binary="+n);
                 {
                 int temp = n;
                 
                 while(n>0)
                 {
                     r=n%10;
                     s=s+(r*p);
                     p=p*2;
                     n=n/10;
                 }
                 System.out.print("converted to Decimal Number="+s);//generates output
     
                 {}// Need ending brackets!
             } // End of main()
            }} // End of class

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Kinsleyy is offline Newbie
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Re: Convert Binary Integer to Decimal

    Any suggestions would help..PS I know there is an api to do this simple also. But I am trying to show the process

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Convert Binary Integer to Decimal

    You need to use multiplication and addition to go from binary to decimal, not modulus and division. You should probably accept the binary as a string.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Kinsleyy is offline Newbie
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Re: Convert Binary Integer to Decimal

    Okay now I have this..What is wrong now. I need to find correct placement and format to inform the user to enter the binary integers..etc
    Code:
    public static long binaryToDecimal(String binary) throws NumberFormatException {
      // Initialize result to 0
      long res = 0;
    
      // Do not continue on an empty string
      if (binary.isEmpty()) {
        throw new NumberFormatException("Empty string is not a binary number");
      }
    
      // Consider each digit in the string
      for (int i = 0; i < binary.length(); i++) {
        // Get the nth char from the right (first = 0)
        char n = binary.charAt(binary.length() - (i+1));
       
        // Check if it's a valid bit
        if ((n != '0') && (n != '1')) {
          // And if not, die horribly
          throw new NumberFormatException("Not a binary number");
        } else if (n == '1') {
          // Only add the value if it's a 1
          res += Math.round(Math.pow(2.0, i));
        }
      }
    
      return res;
    }

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Convert Binary Integer to Decimal

    res = res*2 + n.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Kinsleyy is offline Newbie
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Re: Convert Binary Integer to Decimal

    Where does that need to be

  8. #7
    Kinsleyy is offline Newbie
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Re: Convert Binary Integer to Decimal

    Ok I figured it out!
    Thanks WingedPanther!!

  9. #8
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Convert Binary Integer to Decimal

    No problem
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: Convert Binary Integer to Decimal

    How would I go about saving this thread or what ever is needed to keep it accessible to me ? (I have to do the same sort of thing in class in a few weeks and I think looking this over then would help)

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Convert Binary Integer to Decimal

    It's saved on the server.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed 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. Hexa convert to binary. sum all binary numbers.
    By iera_yahaya in forum C and C++
    Replies: 5
    Last Post: 02-02-2010, 05:58 AM
  3. Using stacks to convert from decimal to binary
    By J-Camz in forum C and C++
    Replies: 5
    Last Post: 06-15-2009, 12:23 PM
  4. MIPS Integer to Decimal(Fraction)/Percent
    By acm1 in forum General Programming
    Replies: 1
    Last Post: 04-01-2008, 07:16 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