網(wǎng)站首頁 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
相關(guān)推薦
- 2022-05-03 如何判斷?.NET?Core?應(yīng)用程序以管理員身份運(yùn)行的_實用技巧
- 2022-12-21 C語言中continue的用法詳解_C 語言
- 2022-05-26 Python借助with語句實現(xiàn)代碼段只執(zhí)行有限次_python
- 2022-11-10 Python3中字符串的常用操作方法及查找方法_python
- 2023-07-04 spring之BeanDefinition
- 2022-02-09 C語言指針用法總結(jié)_C 語言
- 2022-05-17 獲取當(dāng)前機(jī)器注冊到nacos上的機(jī)器ip
- 2023-04-18 C語言計算連續(xù)無序數(shù)組中缺省數(shù)字方法詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支