Python int to binary string?

后端 未结 30 2243
执念已碎
执念已碎 2020-11-22 05:34

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python?

There are a myriad of dec2bin() functions out on Google... But I

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 05:44

    To calculate binary of numbers:

    print("Binary is {0:>08b}".format(16))
    

    To calculate the Hexa decimal of a number:

    print("Hexa Decimal is {0:>0x}".format(15))
    

    To Calculate all the binary no till 16::

    for i in range(17):
       print("{0:>2}: binary is {0:>08b}".format(i))
    

    To calculate Hexa decimal no till 17

     for i in range(17):
        print("{0:>2}: Hexa Decimal is {0:>0x}".format(i))
    ##as 2 digit is enogh for hexa decimal representation of a number
    

提交回复
热议问题