Python编程练习与解答 练习104:十六进制和十进制数字

编写两个程序hex2int()和int2hex()在十六进制数字和十进制之间进行转换。hex2int负责将16进制的数字转换为十进制,而int2hex负责将十进制数字转换为十六进制。确保大小写字母都能正常工作。

# 首先解决 十六进制转化为十进制
def hex2int(a):
    # 首先解决大小写问题 所有字母大写
    c=a.upper()
    # 检索字母 并赋值  逐个累加
    # 首先定义一个总数count
    count=0
    length=len(c)
    for i in c :
        sequence=c.index(i)
        if i == "A":
            num=10*16**(length-1-sequence)
            count+=num
        elif i == "B":
            num=11*16**(length-1-sequence)
            count+=num
        elif i == "C":
            num=12*16**(length-1-sequence)
            count+=num
        elif i == "D":
            num=13*16**(length-1-sequence)
            count+=num
        elif i == "E":
            num=14*16**(length-1-sequence)
            count+=num
        elif i == "F":
            num=15*16**(length-1-sequence)
            count+=num
        elif i == "9":
            num = 9 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "8":
            num = 8 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "7":
            num = 7 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "6":
            num = 6 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "5":
            num = 5 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "4":
            num = 4 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "3":
            num = 3 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "2":
            num = 2 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "1":
            num = 1 * 16 ** (length - 1 - sequence)
            count += num
        elif i == "0":
            num = 0 * 16 ** (length - 1 - sequence)
            count += num
    return count
def int2hex(b):
    six_ten=""
    # 首先对十进制数字进行初步处理
    result = b // 16
    remainder = b % 16
    if remainder == 10:
        six_ten += "A"
    elif remainder == 11:
        six_ten += "B"
    elif remainder == 12:
        six_ten += "C"
    elif remainder == 13:
        six_ten += "D"
    elif remainder == 14:
        six_ten += "E"
    elif remainder == 15:
        six_ten += "F"
    elif remainder < 10:
        str_remainder = str(remainder)
        six_ten += str_remainder
    # 再进一步对结果进行处理
    while result > 16 :
        # 计算余数
        remainder = result % 16
        if remainder == 10:
            six_ten += "A"
        elif remainder == 11:
            six_ten += "B"
        elif remainder == 12:
            six_ten += "D"
        elif remainder == 13:
            six_ten += "C"
        elif remainder == 14:
            six_ten += "E"
        elif remainder == 15:
            six_ten += "F"
        elif remainder < 10:
            str_remainder=str(remainder)
            six_ten+=str_remainder
        # 计算结果
        result = result // 16
    if result < 16 :
            result=str(result)
            six_ten+=result
            end_result=("".join(reversed(six_ten)))
            return end_result
def main():
    # 应先判定转换何种进制
    choose=int(input("请选择转换方法,输入1十六进制转换为十进制,输入2十进制转换为十六进制"))
    if choose==1:   # 十六进制转十进制
        # 输入要转换的数字
        six_num=input("请输入要转换的数字")
        print(hex2int(six_num))
    else:   # 十进制转十六进制
        # 输入要转换的数字
        ten_num = int(input("请输入要转换的数字"))
        print(int2hex(ten_num))
main()

你可能感兴趣的:(Python编程练习与解答,python,开发语言,linux)