-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHexToDen.py
More file actions
36 lines (28 loc) · 873 Bytes
/
HexToDen.py
File metadata and controls
36 lines (28 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
HexNum = input("Enter a hexadecimal number: ")
HexNum = HexNum.lower() #convert the uppercase to lowercase
charSet = HexNum in 'abcdef0123456789' #define valid chars
DenNum = 0 #initial denary value
l = len(HexNum) #number of hex digits
print(HexNum)
#HexNum = HexNum[::-1] #reverse the string
HexNum.
print(HexNum)
for c in range(l):
thisChar = HexNum[c:c+1]
thisDigit = 0
if thisChar == 'a':
thisDigit = 10
elif thisChar == 'b':
thisDigit = 11
elif thisChar == 'c':
thisDigit = 12
elif thisChar == 'd':
thisDigit = 13
elif thisChar == 'e':
thisDigit = 14
elif thisChar == 'f':
thisDigit = 15
elif thisChar in ('0123456789'):
thisDigit = int(thisChar)
DenNum += thisDigit * (16 ** (c))
print(DenNum)