BINARY AND HEX CONVERTOR
WHAT IS THE PROJECT
This section firstly looks at creating a program to convert Binary to Decimal and the second program will convert hex to decimal. The third program in this section uses functions to put it all together to create one neat program with both features.
BINARY TO DENARY CONVERTOR
This mini program asks the user for a binary number and then outputs the denary value of the number. Feel free to copy paste and play with the code.
binary = input("Please enter a binary number: ") end = ((len(binary))-1) #Set a marker to start at the left most digit count = 1 total = 0 for i in range (len(binary)): value = int(binary[end]) end = (end -1) #To shift from the left most digit until the right most digit total = (total + (value * count)) count = count * 2 #To change the value of the column to be calculated next (2,4,8,16 etc) print ("The number you entered is: " + str(total))
Note: to improve this program you could add a check to make sure only 1s or 0s have been typed. You could also add a loop
to ask the user if they want to do another number.
to ask the user if they want to do another number.
HEX TO DENARY CONVERTOR