# Type your text here
defconvert_to_10(s,base):digits="0123456789ABCDEF"s=s.upper()value=0# Check user input is valid number in base
forcharacterins:ifcharacternotindigits[:base]:print("Invalid digit '{}' for base {}".format(character,base))returnNonevalue=value*base+digits.index(character)returnvalue# Function to convert number (n) from base 10 to the given base (2–16).
defconvert_from_10(n,base):ifn==0:return"0"digits="0123456789ABCDEF"result=""whilen>0:result=digits[n%base]+resultn//=basereturnresult#while loop to repeat
whileTrue:# Prompts for user input
print("Universal Base Converter")num_start=input("Enter the number: ")from_base=int(input("Convert from base (2-16): "))to_base=int(input("Convert to base (2-16): "))# Check user input is valid base
iffrom_base<2orfrom_base>16orto_base<2orto_base>16:print("Base must be between 2 and 16")else:# Convert all numbers to base 10
base_10=convert_to_10(num_start,from_base)ifbase_10isnotNone:# Only show Base 10 value if it's not redundant
iffrom_base!=10andto_base!=10:print("Base 10 value:",base_10)# Compute and display the final result
ifto_base==10:# No need to reconvert; just show base 10 value
result=str(base_10)else:result=convert_from_10(base_10,to_base)print("Result:",result)
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.