【python】用python进行进制转换(10进制、2进制、16进制)
生活随笔
收集整理的這篇文章主要介紹了
【python】用python进行进制转换(10进制、2进制、16进制)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
進(jìn)制轉(zhuǎn)換
- ① 16進(jìn)制轉(zhuǎn)10進(jìn)制
- ② 16進(jìn)制轉(zhuǎn)2進(jìn)制
- ③ 10進(jìn)制轉(zhuǎn)16進(jìn)制
- ④ 10進(jìn)制轉(zhuǎn)2進(jìn)制
- ⑤ 2進(jìn)制轉(zhuǎn)16進(jìn)制
- ⑥ 2進(jìn)制轉(zhuǎn)10進(jìn)制
博客由來(lái)寫在前面:由于計(jì)組老師要求我們課后查一下進(jìn)制轉(zhuǎn)換,因?yàn)榈讓拥亩际?1二進(jìn)制進(jìn)行編碼的,故而想到用python進(jìn)行傳統(tǒng)的進(jìn)制轉(zhuǎn)換手推理解,配上python本身的函數(shù)進(jìn)行答案對(duì)比,故有此篇博客。
每一個(gè)進(jìn)制之間轉(zhuǎn)換包含倆種算法,一種是直接調(diào)用python的進(jìn)制轉(zhuǎn)換包,一種是用人工計(jì)算進(jìn)制轉(zhuǎn)換的思路寫的(其中2進(jìn)制轉(zhuǎn)為16進(jìn)制,是采用2進(jìn)制局部轉(zhuǎn)為10進(jìn)制再轉(zhuǎn)為16進(jìn)制的方法)
① 16進(jìn)制轉(zhuǎn)10進(jìn)制
b_16 = '1111'# 方法1 - 調(diào)用函數(shù)法 b_2 = int(b_16, 16) print(b_2)# 方法2 - 按照計(jì)算原則 b_16_len = len(b_16) count = 0 for i in b_16:temp = int(i)count = count + temp*16**(b_16_len-1)b_16_len = b_16_len - 1 print(count)結(jié)果:
4369 4369② 16進(jìn)制轉(zhuǎn)2進(jìn)制
a_16 = '7A9B'# 方法1 - 調(diào)用函數(shù)法 a_2 = bin(int(a_16,16))[2:] print(a_2)# 方法2 - 按照計(jì)算原則 for i in a_16:if i == '1':print('0001',end='')if i == '2':print('0010',end='')if i == '3':print('0011',end='')if i == '4':print('0100',end='')if i == '5':print('0101',end='')if i == '6':print('0110',end='')if i == '7':print('0111',end='')if i == '8':print('1000',end='')if i == '9':print('1001',end='')if i == 'A':print('1010',end='')if i == 'B':print('1011',end='')if i == 'C':print('1100',end='')if i == 'D':print('1101',end='')if i == 'E':print('1110',end='')if i == 'F':print('1111',end='')結(jié)果:
111101010011011 0111101010011011③ 10進(jìn)制轉(zhuǎn)16進(jìn)制
c_10 = 1273# 方法1 - 調(diào)用函數(shù)法 c_16 = hex(c_10) print(c_16)# 方法2 - 按照計(jì)算原則 list = [] while c_10//16 != 0:temp = c_10%16if temp <= 9:list.append(temp)if temp == 10:temp = 'a'list.append(temp)if temp == 11:temp = 'b'list.append(temp)if temp == 12:temp = 'c'list.append(temp)if temp == 13:temp = 'd'list.append(temp)if temp == 14:temp = 'e'list.append(temp)if temp == 15:temp = 'f'list.append(temp)c_10 = c_10//16list.append(c_10%16)for i in list[::-1]:print(i,end='')結(jié)果:
0x4f9 4f9④ 10進(jìn)制轉(zhuǎn)2進(jìn)制
d_10 = 17# 方法1 - 調(diào)用函數(shù)法 d_2 = bin(d_10) print(d_2)# 方法2 - 按照計(jì)算原則 list = [] while d_10//2 != 0:temp = d_10%2list.append(temp)d_10 = d_10//2list.append(d_10%2)for i in list[::-1]:print(i,end='')結(jié)果:
0b10001 10001⑤ 2進(jìn)制轉(zhuǎn)16進(jìn)制
e_2 = '1011011101'# 方法1 - 調(diào)用函數(shù)法 e_16 = hex(int(e_2, 2)) print(e_16)# 方法2 - 按照計(jì)算原則 ## 1、確定有幾個(gè),最前面的1-4單獨(dú)計(jì)算,后面的按照4的倍數(shù)進(jìn)行計(jì)算 e_2_len = len(e_2) # 總長(zhǎng) e_2_first = e_2_len % 4 # 首位1-4 e_2_count = e_2_len // 4 list = [] # 保存# 2->10->16 就不用傳統(tǒng)方法了,上面都寫過(guò)如何分步轉(zhuǎn)換 if e_2_first!=0:head = hex(int(e_2[0:e_2_first], 2))list.append(head)for i in range(e_2_count):temp = e_2[e_2_first+4*i:e_2_first+4*(i+1)] # 4個(gè)為一個(gè)局部list.append(hex(int(temp, 2)))for i in list:print(i[2],end='')結(jié)果:
0x2dd 2dd⑥ 2進(jìn)制轉(zhuǎn)10進(jìn)制
f_2 = '1111'# 方法1 - 調(diào)用函數(shù)法 f_10 = int(f_2, 2) print(f_10)# 方法2 - 按照計(jì)算原則 f_2_len = len(f_2) count = 0 for i in f_2:temp = int(i)count = count + temp*2**(f_2_len-1)f_2_len = f_2_len - 1 print(count)結(jié)果:
15 15總結(jié)
以上是生活随笔為你收集整理的【python】用python进行进制转换(10进制、2进制、16进制)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 项目开发合作协议合同范本
- 下一篇: mysql错误代码对照表较完整