在python中将十六进制转换为十进制
我正在尝试编写一个将十六进制转换为十进制的函数。 我有两个问题。我不能用数字来代替所有的字母。它替换一个字母,然后停止。第二,如何得到它,使它连续地将每个整数相加在python中将十六进制转换为十进制,python,decimal,Python,Decimal,我正在尝试编写一个将十六进制转换为十进制的函数。 我有两个问题。我不能用数字来代替所有的字母。它替换一个字母,然后停止。第二,如何得到它,使它连续地将每个整数相加 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:
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
有一种更简单的方法可以做到这一点: 大的东西:
>>> s = "6f48f8248e828ce82f82"
>>> int(s, 16)
525528725744949635067778L
小一点的:
>>> int('BF', 16)
191
在数字前面添加一个0x
,不会改变最终结果
作为一项功能:
def hex2dec(s):
"""return the integer value of a hexadecimal string s"""
return int(s, 16)
有一种更简单的方法可以做到这一点: 大的东西:
>>> s = "6f48f8248e828ce82f82"
>>> int(s, 16)
525528725744949635067778L
小一点的:
>>> int('BF', 16)
191
在数字前面添加一个0x
,不会改变最终结果
作为一项功能:
def hex2dec(s):
"""return the integer value of a hexadecimal string s"""
return int(s, 16)
我只是在这里玩得很开心,但是要重写你的函数
def toDecimal(hexadecimal):
decimal = int(hexadecimal, 16)
return decimal
toDecimal('0xdeadbeef')
我猜你发明轮子只是为了看看它是怎么做的?:) 我只是在这里玩得很开心,但是要重写你的函数
def toDecimal(hexadecimal):
decimal = int(hexadecimal, 16)
return decimal
toDecimal('0xdeadbeef')
我猜你发明轮子只是为了看看它是怎么做的?:) 您的代码中有很多问题。让我们看一下:
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 )
代码中有很多问题。让我们看一下:
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 )
我认为阅读本惯用python教程会对您有所帮助:
我认为阅读本惯用python教程会对您有所帮助:
这是任务吗?否则int(hexnumber,16)执行作业是的,这是一个赋值。请尝试
int('0xA',16)
或eval('0xA')
这是一个赋值吗?否则int(hexnumber,16)会执行作业是的,这是一个赋值。请尝试int('0xA',16)
或eval('0xA')
这是一个赋值。因此我不允许使用int()。这是一个赋值。因此我不允许使用int()。