【发布时间】:2014-02-19 10:13:46
【问题描述】:
import math
def hexToDec(hexi):
result = 0
for i in range(len(hexi)-1,-1,-1):
if hexi[i] == 'A':
result = result + (10 * math.pow(16,i))
elif hexi[i] == 'B':
result = result + (11 * math.pow(16,i))
elif hexi[i] == 'C':
result = result + (12 * math.pow(16,i))
elif hexi[i] == 'D':
result = result + (13 * math.pow(16,i))
elif hexi[i] == 'E':
result = result + (14 * math.pow(16,i))
elif hexi[i] == 'F':
result = result + (15 * math.pow(16,i))
else:
result = result + (int(hexi[i]) * math.pow(16,i))
return result
即使在反转范围顺序并重新导入之后,我仍然得到相同的结果。
【问题讨论】:
-
为什么
int(hexi,16)不删呢?它似乎能够处理 py3 中的大量数字。 (也许long(hexi,16)在 py2 中工作?)
标签: python hex decimal number-systems