位字符串python [英] bits to string python

查看:90
本文介绍了位字符串python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用此功能将字符串转换为位.

I have used this function to convert string to bits.

def a2bits(chars):
     return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:]

我该如何做相反的事情?位串.我是否需要将这些位分隔为ASCII数字,然后将其转换为字符?

How would I go about doing the reverse? Bits to string. Would I have to separate the bits into ASCII numbers and then convert them into characters?

我从此站点获得了功能 a2bits : http://www.daniweb.com/software-development/python/code/221031/string-to-bits

I got the function a2bits from this site: http://www.daniweb.com/software-development/python/code/221031/string-to-bits

标准库中是否有东西可以将位转换为字符串?

Is there something in the standard library to convert bits to string?

推荐答案

>>> def bits2a(b):
...     return ''.join(chr(int(''.join(x), 2)) for x in zip(*[iter(b)]*8))
... 
>>> bits2a('0110100001100101011011000110110001101111')
'hello'

这篇关于位字符串python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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