首页 > 编程知识 正文

Python 程序将十六进制字符串转换为十进制字符串

时间:2023-11-26 13:13:05 阅读:309313 作者:TKHX

在 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()方法

用户可以使用literal_eval函数将十六进制字符串转换为十进制字符串。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”、“计数”和“j”,并且都等于“0”。之后,他们必须应用包含所有条件的“while”循环。最后,用户可以检查“k”的值。如果“k”的值等于“0”,用户可以打印“计数”的值;否则,它将是无效的输入。

示例:


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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。