How do you express binary literals in Python?

前端 未结 7 1842
囚心锁ツ
囚心锁ツ 2020-11-27 10:15

How do you express an integer as a binary number with Python literals?

I was easily able to find the answer for hex:

>>> 0x12AF
4783
>>         


        
7条回答
  •  没有蜡笔的小新
    2020-11-27 10:41

    Another good method to get an integer representation from binary is to use eval()

    Like so:

    def getInt(binNum = 0):
        return eval(eval('0b' + str(n)))
    

    I guess this is a way to do it too. I hope this is a satisfactory answer :D

提交回复
热议问题