且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python将decimal转换为hex

更新时间:2023-08-28 09:01:04

如果您想自己编写代码而不是使用内置函数 hex(),则可以在打印当前数字之前简单地执行递归调用:

If you want to code this yourself instead of using the built-in function hex(), you can simply do the recursive call before you print the current digit:

def ChangeHex(n):
    if (n < 0):
        print(0)
    elif (n<=1):
        print n,
    else:
        ChangeHex( n / 16 )
        x =(n%16)
        if (x < 10):
            print(x), 
        if (x == 10):
            print("A"),
        if (x == 11):
            print("B"),
        if (x == 12):
            print("C"),
        if (x == 13):
            print("D"),
        if (x == 14):
            print("E"),
        if (x == 15):
            print ("F"),