0

16 進数を 10 進数に変換する関数を作成しようとしています。2 つの問題があります。すべての文字を数字に置き換えることはできません。文字を置き換えてから停止します。次に、各整数を連続して追加するにはどうすればよいですか?

 def toDecimal(hexidecimal):
    decimal=[hexidecimal[i:i+1] for i in range(0,len(hexidecimal), 1)]
    for i in range(0,len(decimal)):
            if 'a' in decimal:
                decimal[i]='10'
            if 'b' in decimal:
                decimal[i]='11'
            if 'c' in decimal:
                decimal[i]='12'
            if 'd' in decimal:
                decimal[i]='13'
            if 'e' in decimal:
                decimal[i]='14'
            if 'f' in decimal:
                decimal[i]='15'
            return decimal
      #Above I try to convert any letters into a number value 
    for i in range(0,len(decimal)):
        converted_decimal=decimal[i]*(16**i)
        total_decimal=converted_decimal+converted_decimal
    return total_decimal
     #Here I'm trying to add each converted decimal 
4

4 に答える 4

2

コードには多くの問題があります。それを見てみましょう:

hexidecimal= "7ac8965f" #hexadecimal value

decimal=[hexidecimal[i:i+1] for i in range(0,len(hexidecimal), 1)]
# >> decimal : ["7","a","c","8","9","6","5","f"]

for i in range(0,len(decimal)):
# first path : i = 0

        # First Error : 'in' is a array-wide search.
        # you want to use :'if decimal[i] == 'a' '
        if 'a' in decimal: # a is in decimal (second pos) so decimal[0] is modified !
            decimal[i]='10'
            # >> decimal : ["10","a","c","8","9","6","5","f"]

        if 'b' in decimal:
            decimal[i]='11'
        if 'c' in decimal:
            decimal[i]='12'
        if 'd' in decimal:
            decimal[i]='13'
        if 'e' in decimal:
            decimal[i]='14'
        if 'f' in decimal: # f is in decimal (last pos) so decimal[0] is modified !
            decimal[i]='15'
            # >> decimal : ["15","a","c","8","9","6","5","f"]

        #Second Error : anticipated return
        #Assuming the indentation is correct, the function exit here, on the 
        #first run of the function
        return decimal

今解決策:

#dict for conversion (there are more elegant ways to do it, 
#                                                        but this is good enough)
conversion_table = { '0' : 0 ,
                     '1' : 1 ,
                     '2' : 2 ,
                     '3' : 3 ,
                      .......
                     'a' : 10 ,
                     'b' : 11 ,
                     ......
                     'f' : 15
                    }

hexidecimal= "7ac8965f" #hexadecimal value

hexa_list=[ digit for digit in hexidecimal]
# same thing as before, just more "elegant"

decimal = [ conversion_table[hex_digit] for hex_digit in hexa_list]
# convert everything in base10

# reduce the list
return reduce( lambda x,y : x*16 + y, decimal )
于 2013-10-15T08:22:23.710 に答える
1

私はここで少し楽しんでいますが、関数を書き直すために...

def toDecimal(hexadecimal):
    decimal = int(hexadecimal, 16)
    return decimal

toDecimal('0xdeadbeef')

それがどのように行われるかを見るためだけに車輪を再発明していると思いますか? :)

于 2013-10-15T08:20:53.120 に答える