Python 進製轉換

2021-09-24 06:43:45 字數 1038 閱讀 9323

print(bin(4)) # 輸出0b100

print(''.format(4)) # 輸出100

複製**

print(oct(8)) #輸出0o10

print(''.format(8)) # 輸出10

複製**

print(hex(15)) # 輸出0xf

print(''.format(15)) # 輸出f

複製**

# int('要轉換的字串',制定進製)

print(int('1010', 2)) # 輸出10

print(eval('0b1010')) # 輸出10

print(''.format(0b1010)) # 輸出10

複製**

print(int('14', 8)) # 輸出12

print(eval('0o14')) # 輸出12

print(''.format(0o14)) # 輸出12

複製**

print(int('10', 16)) # 輸出16

print(eval('0x10')) # 輸出16

print(''.format(0x10)) # 輸出16

複製**

注意

hex()函式格式化字串比format()慢,不推薦使用。

eval()函式比hex慢,不推薦使用。

print(bin(int('0xf', 16))) # 輸出0b1111

複製**

print(bin(0xa)) # 輸出0b1010

print(oct(0xa)) # 輸出0o12

print(hex(0b1010)) #輸出0xa

複製**

Python 進製轉換

python 進製轉換 1 oct hex bin 允許把 整數轉換為其他進製的字串 例子 oct 64 hex 64 bin 64 0100 0x40 0b1000000 oct函式會將十進位制數轉換為八進位制數,hex函式會將十進位制數轉換為十六進製制數,而 bin函式會將十進位制轉換為二進位制...

Python 進製轉換

python手擼實現十進位制轉16 8 2進製 class solution object def init self pass def convert self while true input num input 請輸入乙個整數 輸入q結束程式 if input num q return ten...

Python 進製轉換

print bin 4 輸出0b100 print format 4 輸出100print oct 8 輸出0o10 print format 8 輸出10print hex 15 輸出0xf print format 15 輸出f int 要轉換的字串 制定進製 print int 1010 2 ...