
-----------------------------------
Taruen
Wed May 31, 2006 9:32 pm

Please, Need Help To Convert Decimal To Binary Numbers
-----------------------------------
Hey... i'm lookin for some help in creating a small program that converts decimal or numbers... into binary numbers... any help please.?

-----------------------------------
Tony
Wed May 31, 2006 10:05 pm


-----------------------------------
000 = 0 = 0
001 = 2^0 = 1
010 = 2^1 = 2
011 = 2^1 + 2^0 = 3
100 = 2^2 = 4
101 = 2^2 + 2^0 = 5
111 = 2^2 + 2^1 + 2^0 = 6

notice any patterns?

-----------------------------------
Taruen
Wed May 31, 2006 10:08 pm


-----------------------------------
lol yeah i sorta get it.... but i just need help makin a program... like when i click a command button after entering a number in a text box...  i want it to show on a label.. the binary number of itl... how would i program that?

-----------------------------------
NikG
Thu Jun 01, 2006 12:49 am


-----------------------------------
We can't give the code to you, especially considering this is a common assignment in schools from what I remember.

Tony gave you a clue.  All you gotta do is find a way to go in the opposite direction that Tony showed.

-----------------------------------
Brightguy
Thu Jun 01, 2006 11:35 am

Re: Please, Need Help To Convert Decimal To Binary Numbers
-----------------------------------
As a hint, you'll want to use Mod and integer division  (the \ operator).

Start by finding the least significant bit (the right-most) and then the next least significant, etc.  Then get it in a loop so you find one bit each time through the loop.

-----------------------------------
NikG
Thu Jun 01, 2006 11:37 am


-----------------------------------
I believe I've also managed to successfully do this using string manipulation and a for loop (i.e. no Math operators).

-----------------------------------
Tony
Thu Jun 01, 2006 11:47 am

Re: Please, Need Help To Convert Decimal To Binary Numbers
-----------------------------------
Start by finding the least significant bit (the right-most) and then the next least significant, etc. 
Shouldn't it be the other way around, with the left most?

5 mod 2^2 < 5 -- you've got your first bit (of 3)

-----------------------------------
Brightguy
Thu Jun 01, 2006 4:23 pm

Re: Please, Need Help To Convert Decimal To Binary Numbers
-----------------------------------
Right now I don't see any advantages starting from the most-significant.  But if you wanted to do it like that, why even use Mod?  Just directly compare to powers of 2.

-----------------------------------
Tony
Thu Jun 01, 2006 4:40 pm


-----------------------------------
well yeah, pretty much.

How are you even expecting on finding the least significant bit?

-----------------------------------
Brightguy
Thu Jun 01, 2006 6:54 pm

Re: Please, Need Help To Convert Decimal To Binary Numbers
-----------------------------------
How are you even expecting on finding the least significant bit?
That's what Mod is for.  Even numbers have a 0 for the lsb, odd numbers have a 1.  (I'm guessing you already knew that, though...)

I would say there's often a slight advantage doing it from least-to-most significance, since you don't have to start by finding the starting bit.

-----------------------------------
Tony
Thu Jun 01, 2006 7:05 pm


-----------------------------------
well you'd still need to find the starting bit, otherwise you wouldn't know where to stop :wink: 

you're right, it could be approached from ether direction. Though when starting with lsb, you'd need to bitshift after determining each bit. As oppose to subtracting 2^n from the decimal value. Main difference being the confusion to the student requiring help with conversions :P
