在Python中,与其他编程语言相比,转换元素非常容易。在本教程中,我们将讨论不同的方法,将十六进制字符串转换为十进制字符串。

方法1:使用int()函数

int()函数用于执行这些类型的任务,用户可以在此函数中添加一个参数(16),以将十六进制字符串转换为16进制,同时将字符串转换为整数。

示例:

# First, initialize the string   
testing_string = 'F'  
    
# then, Print the original string   
print ("The Hexadecimal string is: " + str(testing_string))  
    
# now, use the int() function to convert the hexadecimal string to decimal string  
convertion = int(testing_string, 16)  
    
# At last, print result  
print ("The converted hexadecimal string into decimal string is: " + str(convertion))  

输出:

The Hexadecimal string is: F
The converted hexadecimal string into decimal string is: 15

方法2:使用ast.literal_eval()方法

用户可以使用文字评估函数将十六进制字符串转换为十进制字符串。文字评估函数可以预测基数并将数字字符串转换为十进制字符串。

示例:

from ast import literal_eval  
    
# First, initialize the string   
testing_string = '0xF'  
    
# then, Print the original string   
print ("The Hexadecimal string is: " + str(testing_string))  
    
# now, use the ast.literal_eval() function to convert the hexadecimal string to decimal string  
convertion = literal_eval(testing_string)  
    
# At last, print result  
print ("The converted hexadecimal string into decimal string is: " + str(convertion))  

输出:

The Hexadecimal string is: 0xF
The converted hexadecimal string into decimal string is: 15

方法3:使用字典

用户可以使用字典将十六进制字符串转换为十进制字符串。他们可以创建一个包含十六进制表的所有预定义值的字典。然后,用户可以应用“for”循环,并将十六进制字符串值转换为十进制字符串格式。

示例:

# Creating the Dictionary  
dictionary_hexa_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}  
hexadecimal_string = input("Please enter the hexadecimal value: ").strip().upper()  
decimal = 0  
  
length = len(hexadecimal_string) -1  
   
for digit in hexadecimal_string:  
    decimal += dictionary_hexa_to_decimal[digit]*16**length  
    length -= 1  
   
print ("The converted hexadecimal string into decimal string is: ", decimal)

输入:

Please enter the hexadecimal value:  AE

输出:

The converted hexadecimal string into decimal string is:  174

方法4:使用while循环

用户可以使用while循环将十六进制字符串转换为十进制字符串。首先,用户必须导入十六进制字符串值。然后,他们必须使用3个变量:"k","counts"和"j",都等于"0"。然后,他们必须应用包含所有条件的"while"循环。最后,用户可以检查"k"的值。如果"k"的值等于"0",则用户可以打印"counts"的值;否则,它将是无效的输入。

示例:

hexadecimal_string = input("Please enter the hexadecimal value: ")  
   
k = counts = j = 0  
length = len(hexadecimal_string) - 1  
while length >= 0:  
    if hexadecimal_string[length] >= '0' and hexadecimal_string[length] <= '9':  
        rem_1 = int(hexadecimal_string[length])  
    elif hexadecimal_string[length] >= 'A' and hexadecimal_string[length] <= 'F':  
        rem_1 = ord(hexadecimal_string[length]) - 55  
    elif hexadecimal_string[len]>='a' and hexadecimal_string[len]<='f':  
        rem_1 = ord(hexadecimal_string[length]) - 87  
    else:  
        k = 1  
        break  
    counts = counts + (rem * (16 ** j))  
    length = length - 1  
    j = j + 1  
   
if k == 0:  
    print ("\nThe converted hexadecimal string into decimal string is: ", counts)  
else:  
    print ("\nInvalid Input!")  

输入:

Please enter the hexadecimal value:  AF 

输出:

The converted hexadecimal string into decimal string is:  255

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程