Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-05-2009
Member
 
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"); 

) 
)
Reply With Quote
  #2  
Old 20-05-2009
Member
 
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; 
)
Reply With Quote
  #3  
Old 20-05-2009
Member
 
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 ...
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Convert binary to decimal in java"
Thread Thread Starter Forum Replies Last Post
Convert string to binary in java TechGate Software Development 5 28-01-2010 01:36 PM
Convert float to 2 decimal place SoftWore Software Development 3 26-11-2009 01:22 PM
c program to convert decimal to binary Oswaldo Software Development 3 25-11-2009 03:45 PM
Can I convert decimal to string for mssql server? Waffle Software Development 3 06-07-2009 05:56 PM
Convert a string in decimal FlayoFish Software Development 3 23-04-2009 12:18 PM


All times are GMT +5.5. The time now is 04:13 AM.