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
Convert Binary Integer to Decimal
Started by Kinsleyy, Feb 03 2010 06:57 PM
10 replies to this topic
#1
Posted 03 February 2010 - 06:57 PM
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 :)
|
|
|
#2
Posted 04 February 2010 - 01:45 PM
Any suggestions would help..PS I know there is an api to do this simple also. But I am trying to show the process
#3
Posted 04 February 2010 - 03:25 PM
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.
#4
Posted 04 February 2010 - 04:27 PM
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
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;
}
#5
Posted 04 February 2010 - 05:59 PM
res = res*2 + n.
#6
Posted 04 February 2010 - 07:48 PM
Where does that need to be
#7
Posted 04 February 2010 - 09:02 PM
Ok I figured it out!
Thanks WingedPanther!!
Thanks WingedPanther!!
#8
Posted 05 February 2010 - 04:38 AM
No problem :)
#9
Posted 06 February 2010 - 01:25 PM
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)
#10
Posted 06 February 2010 - 07:39 PM
It's saved on the server.
#11
Posted 12 February 2010 - 12:25 PM
Great help for me.


Sign In
Create Account

Back to top









