import javax.swing.JOptionPane;
public class MyDecimal2Binary {
public static void main(String[] args) {
int value;
String message, d2B;
String myInput = JOptionPane.showInputDialog("Enter a positive integer: ");
value = Integer.parseInt(myInput);
d2B = decimalToBinary(value);
message = "Integer number " + myInput + " in Binary is " + d2B;
JOptionPane.showMessageDialog(null, message);
}
public static String decimalToBinary(int value) {
int myResult = value;
String myString;
while (value != 0) {
if (value % 2 == 0)
myResult = myResult *10;
else myResult = (myResult * 10) + 1;
}
myString = Integer.toString(myResult);
return myString;
}
}
Binary to Decimal conversion
Page 1 of 19 Replies - 1839 Views - Last Post: 10 April 2011 - 07:00 PM
#1
Binary to Decimal conversion
Posted 10 April 2011 - 02:03 PM
Hey guys i need help with the syntax of converting a user input decimal integer into a binary string. This is the code i have so far but can't get it to work. Also i cant use any java built in methods to do the integer to binary number conversion. So no binary to string. I'm pretty sure that my problem is in the method in the actual conversion
Replies To: Binary to Decimal conversion
#3
Re: Binary to Decimal conversion
Posted 10 April 2011 - 02:27 PM
Please give more details when asking for help. "It doesn't work," or "can't get it to work," are not helpful.
#4
Re: Binary to Decimal conversion
Posted 10 April 2011 - 03:09 PM
Take a look at this example where 1101B is being converted:
(((((1)*2)+1)*2)+0)*2)+1 = 13
And for 1000B
((((((1)*2)+0)*2)+0)*2)+0 = 8
See the pattern?
EDIT: I'm confused.. in your topic title you write binary to decimal, and in your explaining text you write binary to decimal, but in your code you write decimal to binary :S
(((((1)*2)+1)*2)+0)*2)+1 = 13
And for 1000B
((((((1)*2)+0)*2)+0)*2)+0 = 8
See the pattern?
EDIT: I'm confused.. in your topic title you write binary to decimal, and in your explaining text you write binary to decimal, but in your code you write decimal to binary :S
This post has been edited by CasiOo: 10 April 2011 - 03:27 PM
#5
Re: Binary to Decimal conversion
Posted 10 April 2011 - 04:17 PM
Yea, sorry about that i do mean decimal to binary and thanks for the link softwareEngineer() but i can't use the toBinaryString
#6
Re: Binary to Decimal conversion
Posted 10 April 2011 - 04:43 PM
I did further testing and everything but the method is working
#7
Re: Binary to Decimal conversion
Posted 10 April 2011 - 05:04 PM
The problem iss that with 0 and 1 you will quite fast reach the limit of an int value
1111111111 is the largest int you can have in binary representation
So your myResult = myResult will fail quite fast
you will have to convert it to String right away
Also you forget to divide value by / 2 after testing % 2
1111111111 is the largest int you can have in binary representation
So your myResult = myResult will fail quite fast
you will have to convert it to String right away
Also you forget to divide value by / 2 after testing % 2
public static String decimalToBinary(int value) {
if(value == 0) // special case to have "0" for a value of 0
return "0";
String myString = "";
while (value != 0) {
if (value % 2 == 0)
myString = "0" + myString;
else
myString = "1" + myString;
value /= 2;
}
return myString;
}
#8
Re: Binary to Decimal conversion
Posted 10 April 2011 - 05:19 PM
Thank you much for the help guys!!
#9
Re: Binary to Decimal conversion
Posted 10 April 2011 - 06:18 PM
Would you mind telling us what your solution was? It might be a good thing to have for the future.
Or was it exactly what pbl just said?
Or was it exactly what pbl just said?
#10
Re: Binary to Decimal conversion
Posted 10 April 2011 - 07:00 PM
Actually you can get ride of the initial test for value == 0 by doing
public static String decimalToBinary(int value) {
String myString = "";
do {
if (value % 2 == 0)
myString = "0" + myString;
else
myString = "1" + myString;
value /= 2;
} while (value != 0);
return myString;
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|