在Python中将位转换为字节 [英] Converting bits to bytes in Python

查看:103
本文介绍了在Python中将位转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python 3.x中将位字符串转换为字节字符串.在每个字节中,位从高位到低位填充.如果需要,最后一个字节用零填充.该位字符串最初存储为布尔值或整数(0或1)的集合",我想返回0-255范围内的整数的集合".所谓集合,是指列表或类似的对象,而不是字符串:例如,下面的函数返回一个生成器.

I am trying to convert a bit string into a byte string, in Python 3.x. In each byte, bits are filled from high order to low order. The last byte is filled with zeros if necessary. The bit string is initially stored as a "collection" of booleans or integers (0 or 1), and I want to return a "collection" of integers in the range 0-255. By collection, I mean a list or a similar object, but not a character string: for example, the function below returns a generator.

到目前为止,我能获得的最快速度如下:

So far, the fastest I am able to get is the following:

def bitsToBytes(a):
    s = i = 0
    for x in a:
        s += s + x
        i += 1
        if i == 8:
            yield s
            s = i = 0
    if i > 0:
        yield s << (8 - i)

我尝试了几种选择:使用枚举,建立列表而不是生成器,通过(s<< 1)| x"代替总和来计算s,并且一切似乎都变慢了.由于此解决方案也是我发现的最短和最简单的解决方案之一,因此我对此感到非常满意.

I have tried several alternatives: using enumerate, bulding a list instead of a generator, computing s by "(s << 1) | x" instead of the sum, and everything seems to be a bit slower. Since this solution is also one of the shortest and simplest I found, I am rather happy with it.

但是,我想知道是否有更快的解决方案.尤其是,是否有一个库例程可以更快地完成这项工作,最好是在标准库中?

However, I would like to know if there is a faster solution. Especially, is there a library routine the would do the job much faster, preferably in the standard library?

示例输入/输出

[] -> []
[1] -> [128]
[1,1] -> [192]
[1,0,0,0,0,0,0,0,1] -> [128,128]

在这里,我显示带有列表的示例.发电机会没事的.但是,字符串不会,因此有必要在类似列表的数据之间来回转换字符串.

Here I show the examples with lists. Generators would be fine. However, string would not, and then it would be necessary to convert back and foth between list-like data an string.

推荐答案

最简单的策略来消耗8-er块中的位并忽略异常:

The simplest tactics to consume bits in 8-er chunks and ignore exceptions:

def getbytes(bits):
    done = False
    while not done:
        byte = 0
        for _ in range(0, 8):
            try:
                bit = next(bits)
            except StopIteration:
                bit = 0
                done = True
            byte = (byte << 1) | bit
        yield byte

用法:

lst = [1,0,0,0,0,0,0,0,1]
for b in getbytes(iter(lst)):
    print b

getbytes是一个生成器并接受生成器,也就是说,它可以在大型且可能无限的流中正常工作.

getbytes is a generator and accepts a generator, that is, it works fine with large and potentially infinite streams.

这篇关于在Python中将位转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆