简体   繁体   English

使用字典将十六进制转换为十进制

[英]Converting hexadecimal to decimal using dictionaries

Basically:基本上:

I am trying to convert hex to decimal using dictionaries but I can't figure out how I would convert it.我正在尝试使用字典将十六进制转换为十进制,但我不知道如何转换它。 I have tried using a for loop to iterate over the hexadecimal value that the user enters, and to convert each one by multiplying it to a power of 16 depending on the length of hex.我曾尝试使用 for 循环遍历用户输入的十六进制值,并根据十六进制的长度将其乘以 16 的幂来转换每个值。 For example, if the user enters F21, the program should recognise that the length is 3 so should start by multiplying 15(F) by 16^2 and add this to 2*16^1 and add this to 1*16^0.例如,如果用户输入 F21,程序应识别出长度为 3,因此应首先将 15(F) 乘以 16^2 并将其添加到 2*16^1 并将其添加到 1*16^0。 But I come across so many errors但是我遇到了很多错误

Hexadecimal to Decimal Dictionary:十六进制到十进制字典:

hex_to_decimal = {
    "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"
}

Note:笔记:

I am not trying to use this solution as I am trying to practice dictionaries我不是在尝试使用此解决方案,因为我正在尝试练习字典

s = "F21"
i = int(s, 16)

Since you are converting to decimals, the default base for integers, you should make the values of hex_to_demical integers rather than strings, so that you can perform numeric operations on each translated hex number:由于您要转换为小数,即整数的默认基数,您应该使用hex_to_demical整数而不是字符串的值,以便您可以对每个转换后的十六进制数执行数字运算:

hex_to_decimal = {
    "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
}
def convert(s):
    i = 0
    for h in s:
        i = i *16 + hex_to_decimal[h]
    return i

with the above code,用上面的代码,

convert('F21')

would return:会返回:

3873

A one-liner, using enumerate and sum :单行,使用enumeratesum

def conv(s):
    return sum(16**i*int(hex_to_decimal[x]) for i, x in enumerate(s[::-1]))

conv('FF')
# 255
conv('A1')
# 161
conv('F21')
# 3873

Simply look over both the index i and the digit n of the hex string using enumerate() , and do the computation n_dec*16**i (eg with n_dec = 15 when n = F ).只需使用enumerate()查看十六进制字符串的索引i和数字n ,并计算n_dec*16**i (例如,当n = F时, n_dec = 15 )。 Specifically, we have n_dec = int(hex_to_decimal[n]) , with the int() needed because you store the values of your dict as str s.具体来说,我们有n_dec = int(hex_to_decimal[n]) ,需要int() ,因为您将dict的值存储为str s。 Finally, sum everything up:最后, sum一下:

s_hex = "F21"
s_dec = str(sum(
    int(hex_to_decimal[n])*16**i for i, n in enumerate(reversed(s_hex))
))

The reversed() is needed because the first (hexadecimal) digits refer to the larger values.需要reversed()因为第一个(十六进制)数字指的是较大的值。 The str() is used just to keep the final decimal result as a str , just as the values in your dict . str()仅用于将最终十进制结果保留为str ,就像dict的值一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM