Python内置函数bin() oct()等实现进制转换
发布时间:2021-05-15 05:15:42
使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。
先看Python官方文档中对这几个内置函数的描述:
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
oct(x)
Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
int([number | string[, base]])
Convert a number or string to an integer. If no arguments are given, return 0. If a number is given, return number.__int__(). Conversion of floating point numbers to integers truncates towards zero. A string must be a base-radix integer literal optionally preceded by ‘+' or ‘-‘ (with no space in between) and optionally surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with ‘a' to ‘z' (or ‘A' to ‘Z') having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
hex(x)
Convert an integer number to a hexadecimal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
↓ | 2进制 | 8进制 | 10进制 | 16进制 |
2进制 | - | bin(int(x, 8)) | bin(int(x, 10)) | bin(int(x, 16)) |
8进制 | oct(int(x, 2)) | - | oct(int(x, 10)) | oct(int(x, 16)) |
10进制 | int(x, 2) | int(x, 8) | - | int(x, 16) |
16进制 | hex(int(x, 2)) | hex(int(x, 8)) | hex(int(x, 10)) | - |
bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。


猜你喜欢
- 一、zipfile模块的简述zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频
- 1. A List Apart CSS TopicsA List Apart是一个CSS优秀文章的收集网站,从1999年开始收集文章,关注最
- 函数:string.join()Python中有join()和os.path.join()两个函数,具体作用如下:join(): 连接字符串
- 本文实例为大家分享了JS实现京东快递单号查询的具体代码,供大家参考,具体内容如下<!DOCTYPE html><html
- 在SQL Server中进行开发会让你身处险地,并且寻找快速解决方案。我们编辑了前十名关于SQL Server开发的常见问题。对常见的针对表
- 一、uni.navigateTo(OBJECT)保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面u
- vue提供的了transition组件来实现组件的过渡和路由的过渡,合理使用这个组建可以让我们的页面更加的灵活,提高用户体验。概念在进入/离
- 目录前言全局锁表级锁表锁元数据锁(Metadata Locking,简称:MDL锁)总结参考资料前言在真实的企业开发环境中使用MySQL,M
- 说明: (1)Linux版本Linux version 2.6.32.12-0.7-default (geeko@buildhost) (g
- 思路:先选择在线签名网站,找到接口模拟请求,然后将生成的签名图片显示在 Tkinter 生成的 GUI 窗口上,最后保存生成的签名图片选择网
- Python中pass的作用空语句 do nothing保证格式完整保证语义完整以if语句为例,在c或c++/java中:if(true);
- sys模块在使用python开发脚本的时候,作为一个运维工具,或者是其他工具需要在接受用户参数运行时,这里就可以用到命令行传参的方式,可以给
- 很多网站注册时都会要求输入电子邮箱,其应用场景是比较广的,例如注册账号接收验证码、注册成功通知、登录通知、找回密码验证通知等。本文将介绍如何
- 前言vim是个伟大的编辑器,不仅在于她特立独行的编辑方式,还在于她强大的扩展能力。然而,vim自身用于写插件的语言vimL功能有很大的局限性
- JavaScript中没有Trim函数,VBScript语言中才有这个函数,就是去掉字符串头和尾的空格。您可以访问这篇文章:《增加 java
- ES6为Array增加了of函数用已一中明确的含义将一个或多个值转换成数组。因为,用new Array()构造数组的时候,是有二意性的。构造
- HTTPS介绍HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端
- DIV与TABLE本身并不存在什么优缺点,所谓web标准只是推荐的是正确的使用标签,好比说:DIV用于布局,而TABLE则本来就是转二维数据
- 这两个字符串真实长度和取固定长度的字符串函数是在 photo.163.com 的js文件里看到的。 作者&nb
- 目的:把训练好的pth模型参数提取出来,然后用其他方式部署到边缘设备。Pytorch给了很方便的读取参数接口:nn.Module.param