亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python內(nèi)置進(jìn)制轉(zhuǎn)換函數(shù)的操作

 更新時(shí)間:2021年06月02日 09:46:10   作者:段小胖  
這篇文章主要介紹了python內(nèi)置進(jìn)制轉(zhuǎn)換函數(shù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

看代碼吧~

dec = input('10進(jìn)制數(shù)為:')
print("轉(zhuǎn)換為二進(jìn)制為:", bin(dec))
print("轉(zhuǎn)換為八進(jìn)制為:", oct(dec))
print("轉(zhuǎn)換為十六進(jìn)制為:", hex(dec))
 
string1 = '101010'
print('二進(jìn)制字符串轉(zhuǎn)換成十進(jìn)制數(shù)為:',int(string1,2))
string1 = '367'
print('八進(jìn)制字符串轉(zhuǎn)換成十進(jìn)制數(shù)為:',int(string1,8))
string3 = 'FFF'
print('十六進(jìn)制字符串轉(zhuǎn)換成十進(jìn)制數(shù)為:',int(string1,16))

leetcode第476題:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

class Solution:
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        string = bin(num)
        string1 =''
        for i in range(2,len(string)):
            if string[i] == '1':
                string1 += '0'
            else:
                string1 += '1'                
        return int(string1,2) #二進(jìn)制字符串轉(zhuǎn)換成10進(jìn)制整數(shù)

python各進(jìn)制之間轉(zhuǎn)換函數(shù)

這兩天在研究修正農(nóng)歷庫的事情,搞的很累,想用代碼自動(dòng)完成,于是又把python撿起來了,python還是很好撿的,雖然丟了挺長時(shí)間。

其中就用了python各進(jìn)制轉(zhuǎn)換的問題,寫下來以,備忘。之所以要寫下來,而不是轉(zhuǎn)發(fā),是因?yàn)楹芏嗳藢懙谋容^啰嗦,我只把重點(diǎn)寫出來就可以了,其他全部去掉。

一共用到四個(gè)函數(shù):bin()、oct()、int()、hex()

int():轉(zhuǎn)換為10進(jìn)制;語法:Int(字符串,字符串進(jìn)制) 。例: int("f",16) 輸出為15;int('11',2)輸出為3

即以下三個(gè)函數(shù)都是把10進(jìn)制數(shù)轉(zhuǎn)換成目標(biāo)進(jìn)制。

bin():轉(zhuǎn)換為2進(jìn)制;例:bin( int("f",16) )輸出:'0b1111' .bin(15)同樣輸出'0b1111'。

oct():轉(zhuǎn)換為8進(jìn)制;

hex():轉(zhuǎn)換為16進(jìn)制。

bin()、oct()、hex()的返回值均為字符串,分別帶有0b、0o、0x前綴,后續(xù)處理時(shí)需注意。

以下的x必須為“字符串”,需用引號。

2->8:oct(int(x, 2))

8->2:bin(int(x, 8))

2->16:hex(int(x, 2))

16->2:bin(int(x, 16))

其他用法一樣,就不舉例了。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論