Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions HexToDec.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# Python Hexadecimal to Decimal Conversion
def getDecDigit(digit):
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
return digits.index(digit)

def __getDecDigit(digit):
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F']
for x in range(len(digits)):
if digit == digits[x]:
return x

def hexToDec(hexNum):
decNum = 0
power = 0
for digit in range(len(hexNum), 0, -1):
decNum = decNum + 16 ** power * __getDecDigit(hexNum[digit-1])
power += 1
print(str(decNum))
hexToDec("A5")
decNum = 0
power = 0
for digit in hexNum:
decNum = decNum + 16 ** power * getDecDigit(digit)
power += 1
return decNum

print(hexToDec("A5")) # should print 165