Advertisement
paulogp

dec2bin

Aug 7th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # paulogp
  2. def dec2bin(n):
  3.     ''' converte um numero decimal em binario. retorna string '''
  4.     bStr = ''
  5.  
  6.     if n < 0:
  7.         raise ValueError, "deve inserir um valor positivo!"
  8.  
  9.     if n == 0:
  10.         return '0'
  11.  
  12.     while n > 0:
  13.         bStr = str(n % 2) + bStr
  14.         n = n >> 1
  15.  
  16.     return bStr
  17.  
  18. # exemplo
  19. print(dec2bin(24))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement