python进制转换

0.缘起
最近在写代码跟硬件通信,需要进制转换,发现自己对这块不熟悉,所以特意去看了官方文档
1.转10进制

使用int(x,base=10)函数可以把,二进制,八进制,十六进制,转成十进制
x参数是字符串,base 说明x是什么进制的数据

    >>> int("ff",16)
    255
    >>> int("1000",2)
    8
    >>> int('77',8)
    63
    >>> 

2.转16进制

使用hex(x)函数转16进制,x是一个int整数类型,如果不是整数类型,python会使用__index()__方法返回一个整数类型,
所以
转16进制第一种办法:是八进制跟二进制先转成10进制
第二种办法:传对应的进制正确写法(0b二进制开头,0o八进制开头),python自己转

1.先转成10进制
    >>> hex(15)
    '0xf'
    >>> hex(255)
    '0xff'
    >>> hex(int("1000",2))
    '0x8'
    >>> hex(int("77",8))
    '0x3f'
    >>> 
2.直接转
>>> hex(0b101010)
'0x2a'
>>> hex(0o7)
'0x7'
>>> 
3.转2进制

使用bin(x)函数转2进制,x是一个int整数类型,如果不是整数类型,python会使用__index()__方法返回一个整数类型,
所以
转2进制第一种办法:是8进制跟16进制先转成10进制
第二种办法:传对应的进制正确写法(0x十六进制开头,0o八进制开头),python自己转

1.先转成10进制
>>> bin(8)
'0b1000'
>>> bin(int("ff",16))
'0b11111111'
>>> bin(int("77",8))
'0b111111'
>>> 
2.直接转
>>> bin(0x15)
'0b10101'
>>> bin(0o7)
'0b111'
>>> 

4. 转8进制

使用oct(x)函数转8进制,x是一个int整数类型,如果不是整数类型,python会使用__index()__方法返回一个整数类型,
所以
转8进制第一种办法:是2进制跟16进制先转成10进制
第二种办法:传对应的进制正确写法(0b二进制开头,0x十六进制开头),python自己转

1.先转成10进制
>>> oct(8)
'0o10'
>>> oct(int("ff",16))
'0o377'
>>> oct(int("101010",2))
'0o52'
>>> 
2.直接转
>>> oct(0xff)
'0o377'
>>> oct(0b10101)
'0o25'
>>> 
5.总结
  1. 正常使用肯定会直接转
  2. 忘记了二进制是0b开头,十六进制是0x开头,八进制是0o开头,所有可用int(x,base=10)转成10进制
  3. 这个int里面x是字符串,base是默认10进制,意思是可以把x转成任意的进制,比如3进制,4进制等,但是需要x符号进制要求,你不能把像如下这样
>>> int("99",8)
Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    int("99",8)
ValueError: invalid literal for int() with base 8: '99'
>>> 
6.官方文档
以下函数都是在Built-in Functions里面
hex(x)
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an __index__() method that returns an integer

bin(x)
Convert an integer number to a binary string prefixed with “0b”. 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 prefixed with “0o”. 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



object.__index__(self)
Called to implement operator.index(), and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in bin(), hex() and oct() functions). Presence of this method indicates that the numeric object is an integer type. Must return an integer.

If __int__(), __float__() and __complex__() are not defined then corresponding built-in functions int(), float() and complex() fall back to __index__().

class int([x])
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x defines __int__(), int(x) returns x.__int__(). If x defines __index__(), it returns x.__index__(). If x defines __trunc__(), it returns x.__trunc__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and 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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值