2023.11.04
十进制、二进制(0b)、八进制(0o)、十六进制(0x)
转向十进制,主要用 int()
首先 int() 后面其实带有base参数,缺省时的base=10
即,将传入参数视为十进制值 转成十进制int型,也就是常见的普通的类型转换
当传入参数为数字类型时,实例如下
input1 = 111
input2 = 0b111
input3 = 112
input4 = 0b112 # 此处IDE会报红 0b112不合法
# 直接运行会抛:SyntaxError: invalid digit '2' in binary literal
print(int(input1)) # 等价于int(input1, base=10)
# 数字111转成int型 没变
print(int(input2)) # 数字0b111转成int型
# 默认base=10将输入视为十进制,但这里输入数字类型且以0b开头
# 识别出 输入为二进制数,转换为10进制,输出为 7
# 此时等价于int(input2, base=2)
print(int(input3)) # 数字112转成int型,默认十进制,输出为 112
由上例 input2 可以扩展,当传入参数为数字类型时
可根据约定,指定以0b开头表示二进制,0o开头表示八进制,0x开头表示16进制
缺省base的条件下,由底层识别并调用对应的转换
input1 = 0b111
input2 = 0o17
input3 = 0x1f
print(int(input1)) # 7 等价于int(input1, base=2)
print(int(input2)) # 15 等价于int(input2, base=8)
print(int(input3)) # 31 等价于int(input3, base=16)
=======2023.12.14 补充=========
传入数字是非内置进制(2,8,16)时,比如现在想把9进制数10转成十进制数9
print(int(10, base=9))
# TypeError: int() can't convert non-string with explicit base
只能用字符串
print(int('10', base=9)) # 9
=============================
另起一段,当传入参数为字串时,示例如下
input1 = '111'
input2 = '0b111'
print(int(input1)) # 111 是合法的十进制数 输出数字 111
print(int(input1, base=2)) # 111 是合法的二进制数 输出数字 7
print(int(input2)) # 0b111不是合法的十进制数
# ValueError: invalid literal for int() with base 10: '0b111'
print(int(input2, base=2)) # 0b111是合法的二进制数 输出数字 7
input3 = '112'
input4 = '0b112' # 作为字符串,IDE不认为是二进制数,不报红
print(int(input3)) # 112 是合法的十进制数 输出数字 112
print(int(input3, base=2)) # 112 不是合法的二进制数
# ValueError: invalid literal for int() with base 2: '112'
print(int(input4)) # 0b112 不是合法的十进制数
# ValueError: invalid literal for int() with base 10: '0b112'
print(int(input4, base=2)) # 0b112 不是合法的二进制数
# ValueError: invalid literal for int() with base 2: '0b112'
对于字符串类型的传入参数,必须保证其值合法,才能进行正常转换。
(在int方法的base值指定的进制下)
由此可以得到字符串形式的二进制、八进制、十六进制,向十进制转换:
input1 = '111'
input2 = '0b111'
input3 = '17'
input4 = '0o17'
input5 = '1f'
input6 = '0x1f'
print(int(input1, base=2)) # 7
print(int(input2, base=2)) # 7
print(int(input3, base=8)) # 15
print(int(input4, base=8)) # 15
print(int(input5, base=16)) # 31
print(int(input6, base=16)) # 31
====================== 分割线 ========================
下面是转二进制,转八进制,转十六进制
转二进制, 用bin()
input0 = '0b111'
print(bin(input0)) # bin() 不能接收字符型传入参数
# TypeError: 'str' object cannot be interpreted as an integer
input1 = 11
input2 = 0b11
input3 = 0o11
input4 = 0x11
print(bin(input1)) # 0b1011
print(bin(input2)) # 0b11
print(bin(input3)) # 0b1001
print(bin(input4)) # 0b10001
转八进制,用oct()
input0 = '0o111'
print(oct(input0)) # oct() 不能接收字符型传入参数
# TypeError: 'str' object cannot be interpreted as an integer
input1 = 11
input2 = 0b11
input3 = 0o11
input4 = 0x11
print(oct(input1)) # 0o13
print(oct(input2)) # 0o3
print(oct(input3)) # 0o11
print(oct(input4)) # 0o21
转十六进制,用hex()
input0 = '0x111'
print(hex(input0)) # hex() 不接收字符串参数
# TypeError: 'str' object cannot be interpreted as an integer
input1 = 11
input2 = 0b11
input3 = 0o11
input4 = 0x11
print(hex(input1)) # 0xb
print(hex(input2)) # 0x3
print(hex(input3)) # 0x9
print(hex(input4)) # 0x11
综上,转向二进制,八进制,十六进制分别使用bin(), oct(), hex()
这几者之间可以直接转换,但需要保证传入的是数字类型
返回值类型是字符串
i = 15
print(type(bin(i))) <class 'str'>
print(type(oct(i))) <class 'str'>
print(type(hex(i))) <class 'str'>
所以如果想去转换后的数字部分,直接使用 ret[2:] 把开头的0b/0o/0x截掉即可
以上
参考文章: