当前位置: 首页 > article >正文

十进制转十六进制 ← Python字符串

【知识点】
chr() 函数与 ord() 函数示例:

>>> ord('0')
48
>>> chr(48)
'0'
>>> ord('9')
57
>>> chr(57)
'9'
>>> ord('A')
65
>>> chr(65)
'A'
>>> ord('F')
70
>>> chr(70)
'F'

● 巧妙利用字符串的“连接”功能,实现结果输出。
具体到本例代码中,关键是不能将语句 
hex=chr(t+ord('0'))+hex 写成 hex=hex+chr(t+ord('0'))

【输入样例】
123

【输出样例】
7B

【十进制转十六进制 Python 代码】

def toHex(n):
    hex=""
    while n!=0:
        t=n%16
        if 0<=t<=9:hex=chr(t+ord('0'))+hex
        else:hex=chr(t-10+ord('A'))+hex
        n=n//16

    print(hex)

x=eval(input())
toHex(x)

十进制转二进制 Python 代码

def toBin(n):
    bin=""
    while n!=0:
        t=n%2
        bin=chr(t+ord('0'))+bin
        n=n//2

    print(bin)

x=eval(input())
toBin(x)



【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/134089268
https://blog.csdn.net/hnjzsyjyj/article/details/142204614

 


http://www.kler.cn/news/331108.html

相关文章:

  • 【Spring】Spring Boot项目创建和目录介绍
  • 计算机毕业设计 基于Python的无人超市管理系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
  • C#/.NET/.NET Core技术前沿周刊 | 第 7 期(2024年9.23-9.30)
  • Hive数仓操作(十)
  • MySQL-MySQL访问
  • Spring Boot实现IT知识分享社区
  • 初识 C 语言(2)
  • CF2018C Tree Pruning 题解
  • C--编译和链接见解
  • 中安未来 OCR—— 开启高效驾驶证识别新时代
  • 微服务实战——ElasticSearch(保存)
  • [C++]使用C++部署yolov11目标检测的tensorrt模型支持图片视频推理windows测试通过
  • OpenJudge | Binary Tree
  • kafka发送消费核心参数与设计原理详解
  • 《pyqt+open3d》open3d可视化界面集成到qt中
  • cloud-(Nacos)--注册中心原理-服务注册-服务发现
  • C# Blazor Server 调用海康H5Player播放摄像头画面
  • STM32 实现 UDP 广播通信
  • 从零到一:编写你的第一个PHP API
  • Spring Boot项目中使用MyBatis