Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Created June 6, 2017 06:44
Show Gist options
  • Save mikkipastel/d68170ea40e402b0c2e84e18e5dffd7a to your computer and use it in GitHub Desktop.
Save mikkipastel/d68170ea40e402b0c2e84e18e5dffd7a to your computer and use it in GitHub Desktop.
Digital Circuit Design
def decimal2base(num, base):
result = str()
while (num > 0):
old_num = num
num = int(num/base)
# reminder = int(num%base)
reminder = old_num - (num*base)
result = str(reminder) + result
if (base == 2):
print ("0b" + result)
elif (base == 16):
print ("0x" + result)
else:
print ("base = " + base + ", " +result)
return result
if __name__=="__main__":
decimal2base(37, 2) #bin(37) = 0b100101
decimal2base(25, 2) #bin(37) = 0b11001
decimal2base(32, 16) #hex(32) = 0x20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment