Advertisement
mauricioribeiro

To Decimal

Oct 1st, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. def toDecimal(binario,mostrar=False):
  2.     soma,i =  0,0
  3.     for x in str(binario):
  4.         soma = 2*soma+int(x)
  5.         if mostrar == True : print(x, '+ 2 ^', i, '=',soma)
  6.         if soma ==0: soma = 1
  7.         i+=1
  8.     print(soma)
  9.  
  10. toDecimal(1001,True) #Para usar, chame a função com o seu numero binario, True (se quiser que exiba os calculos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement