MazeDomains

Untitled

Feb 14th, 2020
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. # Base2 to Base10 Converter
  2.  
  3. #set binary test to false
  4. is_binary = False
  5.  
  6. #get original Base 2
  7. while is_binary == False:
  8.  B2 = str(input('What binary number would you like to convert to denary? '))
  9.  
  10. #check is a binary number
  11.  for i in B2:
  12.    if i in ['0','1']:
  13.       is_binary = True
  14.    else :
  15.       print('not a binary number')
  16.       break
  17.  
  18. #copy to variable for conversion
  19. a = B2
  20.  
  21. #get string length
  22. l = (len(B2))
  23.  
  24. #CONTROL
  25. #print (B2)
  26. #print(len(B2))
  27.  
  28. rB2=''.join(reversed(B2)) #reverse order
  29. #CONTROL
  30. #print(rB2)
  31.  
  32. #Maniplate the string
  33.  
  34. #Loop to slice string into components,
  35. #turn them into integers,
  36. #multiply by appropriate exponent of 2
  37. #then add them together
  38.  
  39. c = 0
  40. B10 = 0
  41. while c <= (l-1):
  42.     #CONTROL
  43.     #print (rB2[c])
  44.     B10_new = int(rB2[c])*(2**(c))
  45.     B10 = B10 + B10_new
  46.     #CONTROL
  47.     #print (B10)
  48.     c = c+1
  49.    
  50. print('The Base2 number', B2,'converted to a decimal integer becomes ', B10, )
Add Comment
Please, Sign In to add comment