Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,有没有更好的方法将十进制转换为二进制?_Python_Binary - Fatal编程技术网

在python中,有没有更好的方法将十进制转换为二进制?

在python中,有没有更好的方法将十进制转换为二进制?,python,binary,Python,Binary,我需要从一个整数转换为一个大小为8的列表,它是该数字的二进制表示形式(数字您可以使用,而不是rjust) list(bin(my_num)[2:].zfill(8)) 在Python 2.6或更新版本中,使用格式: Python字符串的一个优点是它们是序列。如果您只需要遍历字符,那么就不需要将字符串转换为列表 编辑:对于隐写术,您可能对将字符流转换为比特流感兴趣。以下是如何使用生成器实现这一点: def str2bits(astr): for char in astr:

我需要从一个整数转换为一个大小为8的列表,它是该数字的二进制表示形式(数字您可以使用,而不是
rjust

list(bin(my_num)[2:].zfill(8))

在Python 2.6或更新版本中,使用
格式

Python字符串的一个优点是它们是序列。如果您只需要遍历字符,那么就不需要将字符串转换为列表

编辑:对于隐写术,您可能对将字符流转换为比特流感兴趣。以下是如何使用生成器实现这一点:

def str2bits(astr):
    for char in astr:    
        n=ord(char)
        for bit in '{0:0=#10b}'.format(n)[2:]:
            yield int(bit)
以及将比特流转换回字符流:

def grouper(n, iterable, fillvalue=None):
    # Source: http://docs.python.org/library/itertools.html#recipes
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    return itertools.izip_longest(*[iter(iterable)]*n,fillvalue=fillvalue)

def bits2str(bits):
    for b in grouper(8,bits):
        yield chr(int(''.join(map(str,b)),2))
例如,您可以像这样使用上述函数:

for b in str2bits('Hi Zvarberg'):
    print b,
# 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 0 0 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 1

# To show bits2str is the inverse of str2bits:
print ''.join([c for c in bits2str(str2bits('Hi Zvarberg'))])
# Hi Zvarberg
此外,guru还使用Python和PIL进行了一些与隐写术相关的实验。您可能会在那里找到一些有用的代码


如果您发现需要更高的速度(并且仍然想用Python编写代码),您可能需要研究使用。

我在这里给出了十进制到二进制转换的程序

print "Program for Decimal to Binary Conversion"

n = 0
bin = 0
pos = 1

print "Enter Decimal Number:", 
n = input()

while(n > 0):
   bin = bin + (n % 2) * pos;
   n = n / 2;
   pos *= 10;

print "The Binary Number is: ", bin       

#sample output
#Program for Decimal to Binary Conversion
#Enter Decimal Number: 10
#The Binary Number is: 1010

以下是十进制到二进制转换的一种方法:

  • 将十进制数除以2
  • 把剩下的记在旁边
  • 把商除以2
  • 重复此操作,直到小数不能再被除
  • 按相反顺序记录余数,得到结果二进制数
可将其编码为:

d=int(raw_input("enter your decimal:"))
l=[]
while d>0:
    x=d%2
    l.append(x)
    d=d/2
l.reverse()
for i in l:
    print i,
print " is the decimal representation of givin binary data."

这就像本周我们遇到的第四个“如何转换为二进制”问题(主要是用不同的语言),这是什么问题?这是家庭作业吗?(这不是批评,我只是感到惊讶;在一般编程中,将数字转换为二进制表示法不会出现太多问题…)我会说不,但可能有人会纠正我。我不确定>>循环是否比bin()快,但您不需要rjust()循环这不是家庭作业。我正在开发一个隐写术应用程序。这可能不是最好的方法,但这是显而易见的方法。是的,但为了更改元素,我需要一个正确的列表?字符串是不可变的。感谢指向隐写术内容的指针。我尽量避免看python中的其他隐写术文章,因为我希望此应用程序成为我的应用程序。我担心如果我查看其他人的解决方案,他们的想法会滑入我的代码,而我找不到自己的解决方案。我有一个运行良好的应用程序,它运行得非常好,我只是尝试进行一些改进。谢谢,我在搜索时错过了此功能。我尝试在我的应用程序中使用此行替换位屏蔽我在上面发布了一个解决方案,它给了我大约20%的平均改进。zfill也给了rjust方法大约2%的平均改进,但在最坏的情况下提高了12%。当然,所有这些数据都是针对我的特定应用程序的,所以我不知道它是否通用,但对我来说已经足够好了!欢迎来到so,谢谢你的pos正在键入答案。请重新格式化您的答案。正如所写,它看起来像是胡言乱语。@RichardErickson实际上我是新来的,所以不知道如何使用正确的标识发布感谢您格式化您的答案。但是,请您解释您的代码,而不仅仅是发布代码。虽然此代码可以回答问题,但提供了额外的上下文了解此代码回答问题的原因和/或方式可提高其长期价值。
print "Program for Decimal to Binary Conversion"

n = 0
bin = 0
pos = 1

print "Enter Decimal Number:", 
n = input()

while(n > 0):
   bin = bin + (n % 2) * pos;
   n = n / 2;
   pos *= 10;

print "The Binary Number is: ", bin       

#sample output
#Program for Decimal to Binary Conversion
#Enter Decimal Number: 10
#The Binary Number is: 1010
d=int(raw_input("enter your decimal:"))
l=[]
while d>0:
    x=d%2
    l.append(x)
    d=d/2
l.reverse()
for i in l:
    print i,
print " is the decimal representation of givin binary data."