我是靠谱客的博主 畅快河马,这篇文章主要介绍python十进制转二进制转换_python十进制转二进制,可指定位数,现在分享给大家,希望可以做个参考。

# convert a decimal (denary, base 10) integer to a binary string (base 2)

# tested with Python24 vegaseat 6/1/2005

def Denary2Binary(n):

'''convert denary integer n to binary string bStr'''

bStr = ''

if n < 0: raise ValueError, "must be a positive integer" if n == 0: return '0' while n > 0:

bStr = str(n % 2) + bStr

n = n >> 1

return bStr

def int2bin(n, count=24):

"""returns the binary of integer n, using count number of digits"""

return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])

# this test runs when used as a standalone program, but not as an imported module

# let's say you save this module as den2bin.py and use it in another program

# when you import den2bin the __name__ names

最后

以上就是畅快河马最近收集整理的关于python十进制转二进制转换_python十进制转二进制,可指定位数的全部内容,更多相关python十进制转二进制转换_python十进制转二进制内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部