"""
python3 进制转换。
参考:https://blog.csdn.net/weixin_43097301/article/details/82975529
知识点:
0.class int(object)
| int([x]) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). 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 the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
1.bin(number, /) -> 二进制的字符串,如:'0b100'。
Return the binary representation of an integer.
>>> bin(4)
'0b100'
2.oct(number, /) -> 八进制字符串,如:'0o10'。
Return the octal representation of an integer.
>>> oct(8)
'0o10'
3.hex(number, /) -> 十六进制的字符串,如:'0x10'。
Return the hexadecimal representation of an integer.
>>> hex(16)
'0x10'
"""
# 一、十进制转换为二、八、十六进制。
# 1.1.十 -> 二
print("1.1.")
print(bin(1))
print(bin(2))
print(bin(3))
print(bin(4))
# 0b1
# 0b10
# 0b11
# 0b100
# 1.2.十 -> 八
print("\n1.2.")
print(oct(7))
print(oct(8))
print(oct(9))
print(oct(10))
# 0o7
# 0o10
# 0o11
# 0o12
# 1.3.十 -> 十六
print("\n1.3.")
print(hex(15))
print(hex(16))
print(hex(17))
print(hex(18))
# 0xf
# 0x10
# 0x11
# 0x12
# 二、其他进制转换为十进制。
# 2.1.其他进制的“数字”转换为十进制。
print("\n2.1.")
print(int(0b10))
print(int(0o10))
print(int(0x10))
# 2
# 8
# 16
# 2.2.带前缀的其他进制的字符串,转换为十进制。
print("\n2.2.")
print(int('0b10', base=0))
print(int('0o10', base=0))
print(int('0x10', base=0))
# 2
# 8
# 16
# 2.3.不带前缀的其他进制的字符串,转换为十进制。
print('\n2.3.')
print(int('10', 2))
print(int('10', 8))
print(int('10', 16))
# 2
# 8
# 16
转载请注明原文地址:https://ipadbbs.8miu.com/read-6527.html