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");
)
)
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;
)
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 ...