N進数変換サンプルコード|Pythonのお勉強
2022/1/10 2022/1/20
N進数の変換のサンプルコードです。
サンプルコード
# Base Number
# Prefix : Base Number
# 0b : 2 - binal
# 0 : 8 - octal
# 0x : 16 - hexadecimal
# Define decimal number
decimal = 10
# From Decimal to Binal
print(f"Dcimal {decimal} is binal {bin(decimal)}")
# From Decimal to Octal
print(f"Dcimal {decimal} is octal {oct(decimal)}")
# From Decimal to Hexadecimal
print(f"Dcimal {decimal} is octal {hex(decimal)}")
# From Binal to Decimal
binal = int("1010", 2)
print(f"Binal 1010 is decimal {binal}")
# From Octal to Decimal
octal = int("0144", 8) # 144 is ok as well
print(f"Octal 0144 is decimal {octal}")
# From Binal to Decimal
hexa = int("AA", 16)
print(f"Hexadecimal AA is decimal {hexa}")
サンプルコード結果
*** Remote Interpreter Reinitialized ***
Dcimal 10 is binal 0b1010
Dcimal 10 is octal 0o12
Dcimal 10 is octal 0xa
Binal 1010 is decimal 10
Octal 0144 is decimal 100
Hexadecimal AA is decimal 170