Results 1 to 9 of 9
Thread: Convert Decimal To Binary
- 08-04-2010, 06:26 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Convert Decimal To Binary
i have the code:
import java.io.*;
public class Convert{
public static void main(String args[]) throws IOException {
int a[] = {0, 1};
int number;
int r;
BufferedReader Input = new BufferedReader( new InputStreamReader(System.in));
System.out.println();
System.out.print("Enter Decimal Number: ");
number = Integer.parseInt( Input.readLine());
System.out.println();
do{
r=number%2;
if(r > 0){
System.out.println(a[1]); //prints the binary 1
}
else{
System.out.println(a[0]); //prints the binary 0
}
number=number / 2;
}while(number>0);
System.out.println();
System.out.print("Read the answer from bottom to top!");
}
}
and the output is:
--------------------------------
Enter Decimal Number: 10
0
1
0
1
Please read from bottom to top!
--------------------------------
Please help me on how to make the output like below using the following codes above. Thanks for those who help me...!
------------------------
Enter Decimal Number: 10
Binary : 1010
------------------------
- 08-04-2010, 07:28 AM #2
Senior Member
- Join Date
- Jun 2010
- Location
- Destiny Islands
- Posts
- 690
- Rep Power
- 0
Think about this: you have four strings (0, 1, 0, 1). If you were to store these in a variable, you could have: "0101". Then you could reverse it to get "1010".
Your method may not be the best but it appears to work for you--so what I'm suggesting is that instead of using println() for each number you print, create an empty string that you prepend each time you have another number. So it would be "0", "10", "010", "1010".
Much luck! :)
- 08-04-2010, 02:06 PM #3
Do you have to print the binary digits as you compute them or can you save them and print them all after they are all available?
If you can save them, use String concatenation to save them them into a String and then print that String when done.
- 08-04-2010, 02:26 PM #4
if you are only interested in the string-result, and not each single bit, you can also replace your do-while-loop with the following code
Java Code:System.out.println("Binary: " + Long.toBinaryString(Long.parseLong(String.valueOf(number))));
- 08-04-2010, 03:18 PM #5
Calling someone else's methods doesn't demonstrate that the student knows how to solve the problem.
- 08-05-2010, 02:43 AM #6
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
I think it is the shortcut method where in it automatically converts the decimal to binary to its correct output position. Our instructor said that we do not use the shortcut method.."
- 08-05-2010, 07:12 AM #7
Senior Member
- Join Date
- Jun 2010
- Location
- Destiny Islands
- Posts
- 690
- Rep Power
- 0
Unfortunately the method you are using can only work this way (in the reverse direction). So you have two choices:
1) Use String prepending (concatenation to the start of the string), then print the string.
2) Use a different method (I can show you a different one).
- 08-06-2010, 07:07 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Zack can you show me the different one method you said? Can you help me cause its an assignment, i am only newbie and i want to learn...i've tried searching about the string prepending or the concatenation you said but i dont have found sample..i'm running out of time for the submission to our instructor..hope you can help me..."
- 08-06-2010, 07:32 AM #9
Senior Member
- Join Date
- Jun 2010
- Location
- Destiny Islands
- Posts
- 690
- Rep Power
- 0
Have you done string concatenation? Like...
Java Code:String x = "a" + "b";
Alternately, here is another type of code you can use:
How to Convert from Decimal to Binary - wikiHow
(The second method is what you used; the first method is what I'm suggesting.)
Similar Threads
-
Binary to Decimal Converter
By c_walker in forum New To JavaReplies: 15Last Post: 11-24-2009, 02:38 PM -
how to convert decimal value into 8-bit binary value
By tOpach in forum New To JavaReplies: 4Last Post: 10-26-2009, 10:17 PM -
Convert binary into decimal
By WarmRegards in forum New To JavaReplies: 8Last Post: 10-18-2009, 02:32 PM -
Eclipse- Decimal to binary
By queen_vee in forum New To JavaReplies: 1Last Post: 02-24-2009, 02:17 PM -
Convert decimal to binary..pls help..newbie here
By mephisto772 in forum New To JavaReplies: 5Last Post: 02-12-2009, 08:17 AM
Bookmarks