日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 Python教程 正文

python中常見進(jìn)制之間的轉(zhuǎn)換方式_python

作者:yuzhang_zy ? 更新時間: 2022-07-15 Python教程

1. 很多情況下需要進(jìn)行不同進(jìn)制之間的轉(zhuǎn)換

其中比較常用到的是python的內(nèi)置函數(shù)進(jìn)行進(jìn)制的轉(zhuǎn)換,一般使用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換的時候是先將控制臺輸入的字符串或者是自定義的字符串先轉(zhuǎn)換為10進(jìn)制然后將10進(jìn)制轉(zhuǎn)換為其他的進(jìn)制,常見的是二進(jìn)制、十進(jìn)制、八進(jìn)制、十六進(jìn)制之間的轉(zhuǎn)換,其中遵循一個原則是:

其他進(jìn)制轉(zhuǎn)為十進(jìn)制使用int函數(shù),其他進(jìn)制轉(zhuǎn)為二進(jìn)制使用bin函數(shù),其他進(jìn)制轉(zhuǎn)為八進(jìn)制使用oct函數(shù),其他進(jìn)制轉(zhuǎn)為十六進(jìn)制轉(zhuǎn)為hex函數(shù),并且是借助于10進(jìn)制作為中間的橋梁進(jìn)行轉(zhuǎn)換,也就是使用到int()函數(shù),

下面的表格反應(yīng)了常見進(jìn)制之間的轉(zhuǎn)換

并且轉(zhuǎn)換為對應(yīng)的權(quán)重之后對應(yīng)的字符串會有相應(yīng)的前綴,二進(jìn)制前綴為0b,八進(jìn)制前綴為0o,十六進(jìn)制前綴為0x?

? 2進(jìn)制 8進(jìn)制 10進(jìn)制 16進(jìn)制
2進(jìn)制 - bin(int(input(),8)) bin(int(input(),10)) bin(int(input(),16))
8進(jìn)制 oct(int(input(), 2)) - oct(int(input(),10)) oct(int(input(),16))
10進(jìn)制 int(input(),2)) int(input(),8) - int(input(),2)
16進(jìn)制 hex(int(input(), 2)) hex(int(input(),8)) hex(int(input(),10)) -

使用內(nèi)置函數(shù)的時候反正是轉(zhuǎn)換為哪一個進(jìn)制就使用一個進(jìn)制對應(yīng)的函數(shù)即可,中間需要先轉(zhuǎn)換為十進(jìn)制(int()函數(shù)),其中進(jìn)制轉(zhuǎn)換涉及到的內(nèi)置函數(shù)有:2進(jìn)制:bin(),8進(jìn)制:oct(),10進(jìn)制:int(),16進(jìn)制:hex()

if __name__ == '__main__':
    # input接收到的是字符串, 使用int函數(shù)定義輸入的是什么進(jìn)制的字符串轉(zhuǎn)換為10進(jìn)制數(shù)字
    print(bin(int(input(), 16)))
    print(int(input(), 10))
    print(oct(int(input(), 10)))
    print(hex(int(input(), 10)))

2. 第二種是使用format函數(shù)進(jìn)行轉(zhuǎn)換

在format中加上b,o,x將其他進(jìn)制的轉(zhuǎn)換為二進(jìn)制、八進(jìn)制或者是十六進(jìn)制

if __name__ == '__main__':
? ? print("{:b}".format(int(input(), 8)))
? ? print("{:o}".format(int(input(), 8)))
? ? print("{:x}".format(int(input(), 8)))

3. 手動轉(zhuǎn)化

10進(jìn)制轉(zhuǎn)換為其他進(jìn)制代碼

class Solution:
? ? # 將十進(jìn)制數(shù)字轉(zhuǎn)換為任意的進(jìn)制(1-16)
? ? def decimalToAny(self, decimal: int, x: int):
? ? ? ? remainder = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
? ? ? ? # 當(dāng)n大于0的時候執(zhí)行循環(huán)
? ? ? ? res = ""
? ? ? ? while decimal:
? ? ? ? ? ? res = remainder[decimal % x] + res
? ? ? ? ? ? decimal //= x
? ? ? ? return res
?
if __name__ == '__main__':
? ? decimal, x = map(int, input().split())
? ? print(Solution().decimalToAny(decimal, x))

其他的進(jìn)制轉(zhuǎn)為10進(jìn)制代碼

class Solution:
? ? # 快速冪: x ** n
? ? def quickPower(self, x: int, n: int):
? ? ? ? res = 1
? ? ? ? while n > 0:
? ? ? ? ? ? if n % 2 == 1:
? ? ? ? ? ? ? ? res *= x
? ? ? ? ? ? x *= x
? ? ? ? ? ? n //= 2
? ? ? ? return res
?
? ? def anyToDecimal(self, s: str, base: int):
? ? ? ? n = len(s)
? ? ? ? res = 0
? ? ? ? for i in range(n):
? ? ? ? ? ? # 數(shù)字, ord函數(shù)獲取字母的ascii值
? ? ? ? ? ? if "0" <= s[i] <= "9":
? ? ? ? ? ? ? ? res += (ord(s[i]) - ord("0")) * self.quickPower(base, n - i - 1)
? ? ? ? ? ? # 16進(jìn)制數(shù)字對應(yīng)的權(quán)重
? ? ? ? ? ? elif "a" <= s[i] <= "f":
? ? ? ? ? ? ? ? res += (ord(s[i]) - ord("a") + 10) * self.quickPower(base, n - i - 1)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? res += (ord(s[i]) - ord("A") + 10) * self.quickPower(base, n - i - 1)
? ? ? ? return res
?
if __name__ == '__main__':
? ? li = input().split()
? ? print(Solution().anyToDecimal(li[0], int(li[1])))

原文鏈接:https://blog.csdn.net/qq_39445165/article/details/114539697

欄目分類
最近更新