Jump to content

Binary Converter

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
G1TN3RD

G1TN3RD

    Newbie

  • Members
  • Pip
  • 8 posts
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:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
G1TN3RD

G1TN3RD

    Newbie

  • Members
  • Pip
  • 8 posts
Yea haha =p i didnt see that thread before i created this one... Ill take this one down... How convenient :D

#4
G1TN3RD

G1TN3RD

    Newbie

  • Members
  • Pip
  • 8 posts
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. =((

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's a mod thing. Maybe you could post your conclusion here to wrap it up.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
G1TN3RD

G1TN3RD

    Newbie

  • Members
  • Pip
  • 8 posts
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!

#7
CallinWire

CallinWire

    Newbie

  • Members
  • Pip
  • 8 posts
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);