0
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

即使在反转范​​围顺序并重新导入之后,我仍然得到相同的结果。

4

7 回答 7

1

虽然可以有这样的美丽答案

x = int("FF0F", 16)

看看原始代码是如何出错的也很重要。更正后的版本应该是:

import math
def hexToDec(hexi):
    result = 0
    for i in range(len(hexi)):
        cur_pow = len(hexi) - i  - 1
        if hexi[i] == 'A':
            result = result + (10 * math.pow(16,cur_pow))
        elif hexi[i] == 'B':
            result = result + (11 * math.pow(16,cur_pow))
        elif hexi[i] == 'C':
            result = result + (12 * math.pow(16,cur_pow))
        elif hexi[i] == 'D':
            result = result + (13 * math.pow(16,cur_pow))
        elif hexi[i] == 'E':
            result = result + (14 * math.pow(16,cur_pow))
        elif hexi[i] == 'F':
            result = result + (15 * math.pow(16,cur_pow))
        else:
            result = result + (int(hexi[i]) * math.pow(16,cur_pow))
    return result

无论您是否以“反向”循环,功率顺序和索引都hexi应该以相反的方向迭代,一个增加另一个减少。

现在您可以忘记这一点并使用其他人建议的答案。

于 2014-01-28T08:24:19.040 回答
0

在python中如果你想重新导入一些东西,你需要重新启动python进程,或者手动复制你在python中更改的文件的内容,或者更方便地使用ipython中的℅cpaste。

重新导入在 python 中无法以您的方式工作。

于 2014-01-28T08:14:06.297 回答
0

太多elifpow ... 只需转换 (result = result * 16)添加 (ord(ch) - ord(...))类似

""" Manual hexadecimal (string) to decimal (integer) conversion
    hexadecimal is expected to be in uppercase
"""
def hexToDec(hexi):
  result = 0;

  for ch in hexi:
    if 'A' <= ch <= 'F':
      result = result * 16 + ord(ch) - ord('A') + 10
    else:
      result = result * 16 + ord(ch) - ord('0')

  return result;
于 2014-01-28T08:16:12.997 回答
0

你观察到你的 for 循环生成的索引了吗?

无论您采用何种方向扫描输入字符串(向前或向后),索引都会0为最左边的数字和len(i)-1最右边的数字生成。因此,当您使用索引来计算 中的“数字位置”时math.pow(16,i),您正在计算输入字符串的第一个字符是最右边(最低有效)的数字。

尝试使用math.pow(16, len(hexi)-1-i)...

进行此校正后,扫描方向(向前或向后)无关紧要。你可以将你的 for 循环重写为for i in range(len(hexi)):.

另外,你知道你不需要导入math模块来计算能力吗?您可以使用**运算符:2**4, 16**i,16**(len(hexi)-1-i)

于 2014-01-28T08:20:23.067 回答
0
hexToDec = lambda hexi: int(hexi,16)

或在 Python 2 中:

hexToDec = lambda hexi: long(hexi,16)

?

于 2014-01-28T08:27:31.390 回答
0

其他人已经展示了快速的方法,但是因为你希望它在一个 for 循环中......你的问题在于你的循环参数,电源需要是字符串的 len - 当前位置 - 1 就像@YS-L在他的回答中也做了,而不是使用 if-else 你有字典!(您也可以'A' <= myCurrentChar <= 'F'改为检查)

import math
def hexToDec(hexi):
    result = 0
    convertDict = {"A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15}
    for i in range(len(hexi)):    
        if str.isdigit(hexi[i]):
            result += int(hexi[i]) * math.pow(16, len(hexi) - i - 1)
        else:
            result += convertDict[hexi[i]] * math.pow(16, len(hexi) - i - 1)

    return int(result)

print hexToDec("FFA")

输出:

4090
于 2014-01-28T08:33:49.103 回答
0

单行 - (不是很可读) - 但适用于小写并处理0x前缀

sum(16**pwr*(int(ch) if ch.isdigit() else (ord(ch.lower())-ord('a')+10))
    for pwr, ch in enumerate(reversed(hexi.replace('0x',''))))
于 2014-01-28T08:43:02.590 回答