龙空技术网

进制转换大解密:Python带你走进数字世界

半个码农python 108

前言:

今天咱们对“8进制转10进制算法步骤”大概比较注意,大家都想要剖析一些“8进制转10进制算法步骤”的相关知识。那么小编也在网摘上搜集了一些有关“8进制转10进制算法步骤””的相关知识,希望各位老铁们能喜欢,大家一起来了解一下吧!

一.缘起

最近在写代码跟硬件通信,需要进行各种进制间的转换,发现自己对这块不熟悉,所以特意去看了官方文档

二.转10进制

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

 >>> int("ff",16) # 16进制转十进制    255    >>> int("1000",2) # 二进制转十进制    8    >>> int('77',8) # 八进制转十进制    63

三.转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'>>>

四.转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'

五. 转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'

六.总结

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'

七.官方文档

以下函数都是在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 integerbin(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 integerobject.__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).

标签: #8进制转10进制算法步骤