Skip to content

Instantly share code, notes, and snippets.

@Adizlois
Last active February 25, 2016 13:05
Show Gist options
  • Save Adizlois/d65543d512b2a6855bc2 to your computer and use it in GitHub Desktop.
Save Adizlois/d65543d512b2a6855bc2 to your computer and use it in GitHub Desktop.
A home-made decimal-binary converter...
"""by Alfonso Diz-Lois"""
def Binary_from_decimal(number,bitcount=None):
Binary=[]
while number>1:
if number%2==0:
Binary.insert(0,0)
if number%2==1:
Binary.insert(0,1)
number=number/2
Binary.insert(0,1)
if bitcount:
dif=bitcount-len(Binary)
if dif!=0:
z=dif*[0]
z.extend(Binary)
Binary=z
print Binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment