[Tutor] How to convert a decimal integer into binary

Alan Gauld alan.gauld at btinternet.com
Tue Sep 19 10:40:59 CEST 2006


> You should be able to find a tutorial on this on-line somewhere.
> 
> you know that
> decimal: 1  = binary: 0001
> decimal: 2 = binary: 0010
> decimal: 4 = binary: 0100
> decimal: 8 = binary: 1000
> 
> Do you see the pattern here?
> It's powers of 2.

You can use the math approach or a slightly simpler way 
from a computing point of view is to convert to octal first 
then print the binary representation of each octal 
number(3 bits). You can store the octal numbers and 
their binary representations in a dictionary - only 8 entries

And you can convert to octal using int()

Some ideas to try...

Alan G.



More information about the Tutor mailing list