鱼C论坛

 找回密码
 立即注册
查看: 4324|回复: 0

[学习笔记] 自己写的10进制转16进制(python)

[复制链接]
发表于 2018-2-8 17:07:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def myhex(x):
  2.     result=0
  3.     temp=[]
  4.     hex1=''
  5.     while x:
  6.         result=x%16
  7.         x=x//16
  8.         if result==10:
  9.             temp.append("A")
  10.         elif result==11:
  11.             temp.append("B")
  12.         elif result==12:
  13.             temp.append("C")
  14.         elif result==13:
  15.             temp.append("D")
  16.         elif result==14:
  17.             temp.append("E")
  18.         elif result==15:
  19.             temp.append("F")
  20.         else:
  21.             temp.append(result)

  22.     while temp:
  23.         hex1+=str(temp.pop())

  24.     return hex1
  25. x=int(input("请输入10进制的数字,将其转换成16进制:"))
  26. print("结果为:0X%s" % myhex(x))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 11:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表