Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Base2 to Base10 Converter
- #set binary test to false
- is_binary = False
- #get original Base 2
- while is_binary == False:
- B2 = str(input('What binary number would you like to convert to denary? '))
- #check is a binary number
- for i in B2:
- if i in ['0','1']:
- is_binary = True
- else :
- print('not a binary number')
- break
- #copy to variable for conversion
- a = B2
- #get string length
- l = (len(B2))
- #CONTROL
- #print (B2)
- #print(len(B2))
- rB2=''.join(reversed(B2)) #reverse order
- #CONTROL
- #print(rB2)
- #Maniplate the string
- #Loop to slice string into components,
- #turn them into integers,
- #multiply by appropriate exponent of 2
- #then add them together
- c = 0
- B10 = 0
- while c <= (l-1):
- #CONTROL
- #print (rB2[c])
- B10_new = int(rB2[c])*(2**(c))
- B10 = B10 + B10_new
- #CONTROL
- #print (B10)
- c = c+1
- print('The Base2 number', B2,'converted to a decimal integer becomes ', B10, )
Add Comment
Please, Sign In to add comment