📜  将十进制转换为十六进制的Python程序

📅  最后修改于: 2022-05-13 01:55:05.438000             🧑  作者: Mango

将十进制转换为十六进制的Python程序

在本文中,我们将学习如何在Python中将十进制值(基数为 10)转换为十六进制值(基数为 16)。

方法一:使用hex()函数

hex()函数是 Python3 中的内置函数之一,用于将整数转换为其对应的十六进制形式。

代码 :

Python3
# Python3 program to illustrate
# hex() function
  
print("The hexadecimal form of 69 is "
      + hex(69))


Python3
# Conversion table of remainders to
# hexadecimal equivalent
conversion_table = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
                    5: '5', 6: '6', 7: '7',
                    8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C',
                    13: 'D', 14: 'E', 15: 'F'}
  
  
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    hexadecimal = ''
    while(decimal > 0):
        remainder = decimal % 16
        hexadecimal = conversion_table[remainder] + hexadecimal
        decimal = decimal // 16
  
    return hexadecimal
  
  
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))


Python3
# Conversion table of remainders to
# hexadecimal equivalent
conversion_table = {0: '0', 1: '1', 2: '2', 3: '3',
                    4: '4', 5: '5', 6: '6', 7: '7',
                    8: '8', 9: '9', 10: 'A', 11: 'B',
                    12: 'C', 13: 'D', 14: 'E', 15: 'F'}
  
  
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    if(decimal <= 0):
        return ''
    remainder = decimal % 16
    return decimalToHexadecimal(decimal//16) + conversion_table[remainder]
  
  
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))


输出:

The hexadecimal form of 69 is 0x45

方法二:迭代法

将十进制转换为十六进制的常规方法是将其除以 16,直到它等于 0。给定十进制数的十六进制版本是十六进制形式的从最后到第一的余数序列。要将余数转换为十六进制形式,请使用以下转换表:

RemainderHex Equivalent
00
11
22
33
44
55
66
77
88
99
10A
11B
12C
13D
14E
15F

代码 :

蟒蛇3

# Conversion table of remainders to
# hexadecimal equivalent
conversion_table = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
                    5: '5', 6: '6', 7: '7',
                    8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C',
                    13: 'D', 14: 'E', 15: 'F'}
  
  
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    hexadecimal = ''
    while(decimal > 0):
        remainder = decimal % 16
        hexadecimal = conversion_table[remainder] + hexadecimal
        decimal = decimal // 16
  
    return hexadecimal
  
  
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))

输出:

The hexadecimal form of 69 is 45

方法 3:递归方法

这个想法类似于迭代方法中使用的想法。

代码 :

蟒蛇3

# Conversion table of remainders to
# hexadecimal equivalent
conversion_table = {0: '0', 1: '1', 2: '2', 3: '3',
                    4: '4', 5: '5', 6: '6', 7: '7',
                    8: '8', 9: '9', 10: 'A', 11: 'B',
                    12: 'C', 13: 'D', 14: 'E', 15: 'F'}
  
  
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    if(decimal <= 0):
        return ''
    remainder = decimal % 16
    return decimalToHexadecimal(decimal//16) + conversion_table[remainder]
  
  
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))

输出:

The hexadecimal form of 69 is 45