I have an assignment from my computer science teacher to make a program that converts a binary number to hexadecimal using Python. I have made it so that the binary properly converts to base ten/decimal and the decimal properly converts to hex, but it only works if the binary number is less than 5 digits (i.e. 01101 correctly turns out 16 and 11111 turns 1F, but something like 11010110 stupidly becomes "6") Here's my code:
def main():
print("Convert binary numbers to hexadecimal.")
binary = list(input("Enter a binary number: ")) # User input
for i in range(len(binary)): # Convert binary to list of integers
binary[i] = int(binary[i])
baseten = 0
for i in range(len(binary)): # Converts binary to base ten
baseten = int(baseten + binary[i] * 2**i)
x = int(0)
while 16**(x+1) < baseten: # Determines beginning value of exponent
x+=1
while x >= 0:
value = int(baseten/16**x)
if value < 10:
print(value, end="")
if value == 10:
print("A", end="")
if value == 11:
print("B", end="")
if value == 12:
print("C", end="")
if value == 13:
print("D", end="")
if value == 14:
print("E", end="")
if value == 15:
print("F", end="")
baseten-=int(16**x)
x-=1
NOTE - I know Java pretty well, I've had over a year of experience with it, so don't expect not to understand something that is a bit complicated. Also the goal is not just to use the hex() function.