Results 1 to 3 of 3

Thread: Convert binary to decimal in java

  1. #1
    Join Date
    May 2008
    Posts
    372

    Convert binary to decimal in java

    Hi,

    I am the beginner in Programming, I want to convert binary numbers to decimals. I have made a program but don't know where I am wrong.

    Please let me know, what's wrong in the coding !!
    Code:
    =============================================== while 
    / / 12/31/00 Loop example 1 Java with 
    import java.lang .*; 
    import java.io. *; 
    
    public class Loop1 ( 
    public static void main (String args []) throws IOException 
    ( 
    int binary, temp, count, dec; 
    byte buffer [] = new byte [10]; 
    binary = 0; 
    count = 1; 
    
    System.out.print ( "Enter 10 base decimal to convert to Binary:"); 
    
    System.in.read (buffer); 
    String strInput = new String (buffer); 
    dec = Integer.parseInt (strInput.trim ()); 
    temp = dec; 
    
    while ((dec / 2)! = 0) ( 
    if ((dec% 2) == 1) ( 
    binary = binary + count; 
    ) 
    count = count * 10; 
    dec = dec / 2; 
    ) 
    
    if (temp! = 0) 
    binary = binary + count; 
    
    System.out.println (temp + "Decimal equals to" + binary + "Binary digit"); 
    ) 
    ) 
    ============================================== do while 
    / / 01/10/2001 Loop example 2 java with (do while) 
    
    import java.lang .*; 
    import java.io. *; 
    
    public class Loop2 ( 
    public static void main (String args []) throws IOException 
    ( 
    int binary, temp, count, dec; 
    byte buffer [] = new byte [10]; 
    binary = 0; 
    count = 1; 
    
    System.out.print ( "Enter 10 base decimal to convert to Binary:"); 
    
    System.in.read (buffer); 
    String strInput = new String (buffer); 
    dec = Integer.parseInt (strInput.trim ()); 
    temp = dec; 
    
    
    do ( 
    count = count * 10; 
    dec = dec / 2; 
    if ((dec% 2) == 1) ( 
    binary = binary + count; 
    
    
    ) 
    ) 
    while ((dec / 2)! = 0); 
    
    
    if (temp! = 0) 
    binary = binary + count; 
    
    System.out.println (temp + "Decimal equals to" + binary + "Binary digit"); 
    
    ) 
    ) 
    ============================================== for 
    / / 01/10/2001 Loop example 3 java with (for) 
    
    import java.lang .*; 
    import java.io. *; 
    
    public class Loop3 ( 
    public static void main (String args []) throws IOException 
    ( 
    
    int binary, temp, count, dec; 
    byte buffer [] = new byte [10]; 
    binary = 0; 
    
    System.out.print ( "Enter 10 base decimal to convert to Binary:"); 
    
    System.in.read (buffer); 
    String strInput = new String (buffer); 
    dec = Integer.parseInt (strInput.trim ()); 
    temp = dec; 
    
    for (count = 1; ((dec / 2)! = 0); count = count * 10) 
    ( 
    dec = dec / 2; 
    
    if ((dec% 2) == 1) ( 
    binary = binary + count; 
    ) 
    ) 
    
    if (temp! = 0) 
    binary = binary + count; 
    
    System.out.println (temp + "Decimal equals to" + binary + "Binary digit"); 
    
    ) 
    )

  2. #2
    Join Date
    May 2008
    Posts
    215

    Re: Convert binary to decimal in java

    Hi,

    It could be :
    Code:
    / / Loop2.java in the 
    do ( 
    if ((dec% 2) == 1) ( 
    binary = binary + count; 
    ) 
    count = count * 10; 
    dec = dec / 2; 
    
    ) While ((dec / 2)! = 0); 
    
    - If the location changed, please contact 
    
    
    / / Loop3.java in the 
    
    for (count = 1; ((dec / 2)! = 0); count = count * 10) ( 
    if ((dec% 2) == 1) ( 
    binary = binary + count; 
    ) 
    dec = dec / 2; 
    )

  3. #3
    Join Date
    May 2008
    Posts
    209

    Re: Convert binary to decimal in java

    Only 10 decimal numbers, if you want to output to 2 decimal

    Code:
    Integer.toString (num, radix);
    Changed and modified numbers when trying to enter the decimal value is.

    So, the type string,

    Code:
    Integer.parseInt (stringNumber, 2);
    Use this number changes to 2 decimal string.

    For more references and the
    Code:
    Integer.toBinaryString (num); 
    Integer.toOctalString (num); 
    Integer.toHexString (num);
    So I ...

Similar Threads

  1. Convert string to binary in java
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 01:36 PM
  2. Convert float to 2 decimal place
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 01:22 PM
  3. c program to convert decimal to binary
    By Oswaldo in forum Software Development
    Replies: 3
    Last Post: 25-11-2009, 03:45 PM
  4. Can I convert decimal to string for mssql server?
    By Waffle in forum Software Development
    Replies: 3
    Last Post: 06-07-2009, 05:56 PM
  5. Convert a string in decimal
    By FlayoFish in forum Software Development
    Replies: 3
    Last Post: 23-04-2009, 12:18 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
  •  
Page generated in 1,713,480,539.20684 seconds with 17 queries