I have to create a program in Python that will convert a hexadecimal number to a decimal number, for example from FA2
to 4002
.
I have to write the function hexa2dec(c)
where c
is a string input parameter and the returned value is of type int
.
I've started by creating a dictionary :
dic={"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "A":10, "B":11, "C":12, "D":13, "E":14, "F":15}
and my code is this:
c = "F"
def hexa2dec(c):
d = ""
if c[0]=="F":
d = dic["F"]*(16**(len(c)-1))
return d
But I know that it's very bad, for example I don't know what will the length that a person can enter if this person for example does print(hexa2dec(F4CF))
.
I know that with len(i)
I can know the length but does it help ?