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

问题描述 投票:0回答:1

我将以我不是专业人士开始,并且不确定这是代码问题还是计算机理论问题。我编写了一个Python程序,将标准整数转换为十六进制。似乎一直工作到数字100,000,000,000,000之后,它似乎并没有给我正确的数字,而只是给了我64000000000000L。我已经在x2 64位PC的Windows 10笔记本电脑和台式机上进行了尝试。感谢您的任何帮助。

This is the program output code.

Enter a number:     1
======================================
hexadecimal: 1

Enter a number:     10
======================================
hexadecimal: A

Enter a number:     100
======================================
hexadecimal: 64

Enter a number:     1000
======================================
hexadecimal: 3E8

Enter a number:     10000
======================================
hexadecimal: 2710

Enter a number:     100000
======================================
hexadecimal: 186A0

Enter a number:     1000000
======================================
hexadecimal: F4240

Enter a number:     10000000
======================================
hexadecimal: 989680

Enter a number:     100000000
======================================
hexadecimal: 5F5E100

Enter a number:     1000000000
======================================
hexadecimal: 3B9ACA00

Enter a number:     10000000000
======================================
hexadecimal: 2540BE400

Enter a number:     100000000000
======================================
hexadecimal: 174876E800

Enter a number:     1000000000000
======================================
hexadecimal: E8D4A51000

Enter a number:     10000000000000
======================================
hexadecimal: 6400000000000L

Enter a number:     10000000000000
======================================
hexadecimal: 6400000000000L


Enter a number:     100000000000000
======================================
hexadecimal: 64000000000000L

Enter a number:     100000000000000000000
======================================
hexadecimal: 64000000000000000000L

This is the code for the program. 

#Writen for Python 3
#Decimal number to Hexadecimal

#---------------------------------------------------------
#Variables

#Variable(int) The decimal number you want to convert to a hexadecimal.
original_number_int = input("Enter a number:     ")
#List for storing the whole numbers.
whole_number_list = []
#List for storing decimal remainders.
remainder_decimal_list = []
#list of the whole remainders in 
remainder_list_whole = []
hexdec = remainder_list_whole
#---------------------------------------------------------

#Functions

#Returns a list of float decimals based on the original int entered.
def get_list(original_number_int,whole_number_list,remainder_decimal_list):
    x = original_number_int
    original_number_list = []
    original_number_list.append(original_number_int)
    while x > 0:
        x = float(x)
        x = x / 16
        x = str(x)
        x = x.split('.')
        whole = x[0]
        dec = x[1]
        remainder_decimal_list.append(dec)
        x = x[0]
        x = int(x)

    return remainder_decimal_list
#Converts the float decimal list to whole numbers and reverses it.
def dec_to_whole(remainder_decimal_list,remainder_list_whole):
    x = len(remainder_decimal_list)
    while x != 0:
        x = x - 1
        v = remainder_decimal_list[x]
        v = "." + v
        v = float(v)
        whole_remainder = v * 16
        whole_remainder = int(whole_remainder)
        remainder_list_whole.append(whole_remainder)


    return remainder_list_whole
#Converts remainder_whole_list to hexadecimal
def convert_to_hex(remainder_list_whole,hexdec):

    x = len(remainder_list_whole)
    x = x - 1

    #Converts 10, 11, 12, 13, 14, 15 into A,B,C,D,E,F
    while x  >= 0:
        hexdec = remainder_list_whole[x]
        hexdec = str(hexdec)

        if hexdec == '10':
            remainder_list_whole[x] = 'A'
        elif hexdec == '11':
            remainder_list_whole[x] = 'B'
        elif hexdec == '12':
            remainder_list_whole[x] = 'C'
        elif hexdec == '13':
            remainder_list_whole[x] = 'D'
        elif hexdec == '14':
            remainder_list_whole[x] = 'E'
        elif hexdec == '15':
            remainder_list_whole[x] = 'F'
        else:
            remainder_list_whole[x] = remainder_list_whole[x]
        x = x - 1


    hexdec = remainder_list_whole

    return remainder_list_whole
    return hexdec

#Converts the List to a regular hex number
def format(hexdec):
    hexdec = str(hexdec)
    hexdec = hexdec.replace('[','')
    hexdec = hexdec.replace(']','')
    hexdec = hexdec.replace(',','')
    hexdec = hexdec.replace('\'','')
    hexdec = hexdec.replace(' ','')

    return hexdec

#---------------------------------------------------------
#Main Program

get_list(original_number_int,whole_number_list,remainder_decimal_list)
dec_to_whole(remainder_decimal_list,remainder_list_whole)
convert_to_hex(remainder_list_whole,hexdec)
hexdec = format(hexdec)

print("======================================")
print("hexadecimal: " + hexdec)
python-3.x hex pc
1个回答
0
投票

在Python中,如果我输入错了,请纠正我,但是您不能只使用hex()函数吗?

© www.soinside.com 2019 - 2024. All rights reserved.