请帮助!!!
def dec2hex(n):
x1 =0
counter = 0
answer = ""
if n<=0:
answer =answer + "0"
else:
while (n >16):
counter +=1
n = n /16
x = n
if(x <16 ):
x = int(n)
break
else:
continue
if ((n-x) *16 <16 ):
counter1 = 1
else:
counter1 = counter -1
rem = (n-x) * (16**(counter1))
if rem >16:
while (n >16):
rem = rem /16
x1 = rem
if(x1 <16 ):
x1 = int(rem)
break
else:
continue
if n < 10:
answer =answer + str(int(x))
if (rem ==10 or x1 ==10):
answer = answer + "A"
if (rem ==11 or x1 ==11):
answer = answer + "B"
if (rem ==12 or x1 ==12):
answer = answer + "C"
if (rem ==13 or x1 ==13):
answer = answer + "D"
if (rem ==14 or x1 ==14):
answer = answer + "E"
if (rem ==15 or x1 ==15):
answer = answer + "F"
print(counter,rem,x1,n,counter,x)
return answer
DEC2HEX(2000)
答案 0 :(得分:0)
我想提出这个问题的方法。 while循环用于循环主分区和提醒微积分 for循环用于反转最终答案字符串
while循环条件排除了输入0的情况,因为除数余数将始终为零,循环将为无穷大。 在这种情况下,答案将被第一个if强制为0。
UIViewController
打印(DEC2HEX(2000))
答案 1 :(得分:0)
我能想到的最优雅的答案是格式化字符串文字,它将为您转换值
3.6+(f弦)
>>> d = 2000
>>> print(f'{d:X}')
7D0
3.6之前的版本(字符串格式功能)
>>> d = 2000
>>> print('{:X}'.format(d))
7D0
https://docs.python.org/3/reference/lexical_analysis.html#f-strings