输入一个十进制数,将其转换为十六进制数并输出。
def decimalToHex(decValue
):
hex=""
while decValue
!=0:
hexValue
=decValue
%16
hex=toHexChar
(hexValue
)+hex
decValue
=decValue
//16
return hex
def toHexChar(hexValue
):
if 0<=hexValue
<=9:
return chr(hexValue
+ord('0'))
else:
return chr(hexValue
-10+ord('A'))
def main():
decValue
=eval(input("请输入一个十进制数:"))
print(decValue
,"的十六进制数为:",decimalToHex
(decValue
))
main
()
运行截图:
转载请注明原文地址:https://tech.qufami.com/read-5920.html