Click to See Complete Forum and Search --> : Binary to Decimal
Crystine
May 8th, 2004, 07:27 PM
I need to write a code in Java to convert Binary to Decimal, this is what I have so far, but it doesn't produce the right answer, HELP!!!
import javax.swing.JOptionPane;
public class Binary{
public static void main(String args[]){
String input;
int x;
int m;
int ctr = 0;
int result = 0;
input = JOptionPane.showInputDialog("Enter binary number, up to 16 digits:");
x= Integer.parseInt( input );
while(x > 0 && ctr <= 16){
m = x % 10;
if(m == 0)
result = result + 0;
if(m == 1)
result = 2 ^ ctr + result;
x = x / 10;
ctr++;
}
JOptionPane.showMessageDialog(null, "The Binary Number" + input +" is equal to " +
result);
System.exit( 0 );
}
}
mikeBarr81
May 9th, 2004, 08:50 AM
well the first problem I see is that you parse the string to an integer, which you don't want to be doing. Parsing a string to an integer turns the string directly into an integer (if possible), but it won't convert it from binary.
The best way to do this would be to turn the string into a character array (the String class has a method called something like toCharArray()), and then work on each character individualy. Your for loop shouldn't be hard coded to run 16 times, it should just run until it runs out of digits. You can extract the number of digits from the character array. I'll write some psuedo-code that you should be able to use to get it to work.
String input = //get input here
char[] binaryDigits = input.toCharArray();
for (int i = 0; i is less than the array length; i++)
{
// if number is not 1 or 0 the number is not binary...method
should end.
// else if number is 1 add 2^i to the integer
}
//no need to do anything at all if the character is 0. What is the point in adding 0 to something?
dlorde
May 9th, 2004, 01:46 PM
The best way to do this would be to turn the string into a character array (the String class has a method called something like toCharArray()), and then work on each character individualyHmm, I guess it depends what you mean by 'best'...
I would recommend using the facilities provided in the JDK, for example: String binary = "1010101";
int decimal = Integer.parseInt(binary, 2);
System.out.println(binary + " in decimal = " + decimal);Outputs:
1010101 in decimal = 85
No matter how far down the wrong road you have gone, turn back now...
Turkish proverb
mikeBarr81
May 9th, 2004, 04:35 PM
Hmm, I guess it depends what you mean by 'best'...
I would recommend using the facilities provided in the JDK, for example:
True, but I was assuming it was an assignment that needed to be done without library code. If not go with Dlordes suggestion.
dlorde
May 9th, 2004, 04:51 PM
I was assuming it was an assignment that needed to be done without library codeYes, that's what I meant by 'it depends' - on reading it back I can see might look a bit sarcastic... I didn't mean it that way :blush:
Who dares to teach must never cease to learn...
J. C. Dana
mikeBarr81
May 9th, 2004, 04:58 PM
Thats ok, I didn't think you meant it like that :)
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.