Hello everyone,
Git here...
I know this is an amazingly simple question for most of you...
but i am having some trouble, you see, im in a programming class in college at the moment and we have been given an assignment to make a converter for numbers in base 10 to base 2
So for example
Input: 12,000
Output: 10111011100000
I understand how base2 and 10 works, how to calculate and get the base2 value for a base10 number... Thats easy...
But i'm not too sure how to make Java do this:confused:...
i know i will have to make a while loop so it can go through the stages from the first number to the last...
I will have to obviously make the number divide by two and get the remainder, then sys.out.print that... (Its not going to be in a graphic of any kind, ima just be lazy and make the output come out on a CMD)... '
So i need a while loop, and something that will allow me to get the remainder of a number being divided by 2 several several times...
which brings me to my question... Is there an easier way to constantly divide a number by 2 and capture all the remainders than the several loops im going to have to set, maybe im over complicating things, but from where im sitting, it looks like this will take forever :thumbdown:
Binary Converter
Started by G1TN3RD, Jan 12 2010 08:01 AM
6 replies to this topic
#1
Posted 12 January 2010 - 08:01 AM
|
|
|
#2
Posted 12 January 2010 - 08:40 AM
It looks like you've looked at this thread: http://forum.codecal...html#post231940
Without looking at code, did you have a more specific question?
Without looking at code, did you have a more specific question?
#3
Posted 12 January 2010 - 08:51 AM
Yea haha =p i didnt see that thread before i created this one... Ill take this one down... How convenient :D
#4
Posted 12 January 2010 - 08:55 AM
Ok...well...i would take this one down... But i cant seem to figure out how, and i didnt see like an FAQ on the website anywhere instructing me how.
Can like only mods take down threads? or am i just stupid and missing how to do it. =((
Can like only mods take down threads? or am i just stupid and missing how to do it. =((
#5
Posted 12 January 2010 - 08:58 AM
It's a mod thing. Maybe you could post your conclusion here to wrap it up.
#6
Posted 12 January 2010 - 09:04 AM
Ah, very well then... My conclusion is as follows..
Simplest way to do this, is to make 2while loops, and 1 loop, one for input, one for processing, one for output..
Two strings, one ortaining possible combinations for base10 numbers, this will allow input to be more conveniently organized.. rather than having a int variable for each possible base 10 numbers...
The second loop will simply divide each number in the base10 number by 2 getting the remainder by a second variable that i will setup.. then all that's left to do is make the third loop simply going through sys.out.printing every thing the remainder int variable picks up...
awesome!
Simplest way to do this, is to make 2while loops, and 1 loop, one for input, one for processing, one for output..
Two strings, one ortaining possible combinations for base10 numbers, this will allow input to be more conveniently organized.. rather than having a int variable for each possible base 10 numbers...
The second loop will simply divide each number in the base10 number by 2 getting the remainder by a second variable that i will setup.. then all that's left to do is make the third loop simply going through sys.out.printing every thing the remainder int variable picks up...
awesome!
#7
Posted 12 January 2010 - 11:19 AM
If you understand how to convert decimal to binary, then this should be easy to understand (and edit for your own purposes). Will not work for negatives.
int decimal = 12000; // the number to convert (will not be saved)
String binary = ""; // the binary result will be stored here
while(decimal != 0) {
// add the remainder in this step to the result
// (decimal % 2) will either be 0 or 1
binary = Integer.toString(decimal % 2) + binary;
// divide by 2 for the next iteration
decimal /= 2;
}
// print the result
System.out.println(binary);


Sign In
Create Account

Back to top









