10

I have a binary like this: 1101100110000110110110011000001011011000101001111101100010101000

and I want to convert it to utf-8. how can I do this in python?

4

4 回答 4

14

更清洁的版本:

>>> test_string = '1101100110000110110110011000001011011000101001111101100010101000'
>>> print ('%x' % int(test_string, 2)).decode('hex').decode('utf-8')
نقاب

逆(来自@Robᵩ的评论):

>>> '{:b}'.format(int(u'نقاب'.encode('utf-8').encode('hex'), 16))
1: '1101100110000110110110011000001011011000101001111101100010101000'
于 2013-10-08T19:14:09.313 回答
4

好吧,我的想法是: 1. 将字符串拆分为八位字节 2. 使用int和稍后将八位字节转换为十六进制chr 3. 加入它们并将 utf-8 字符串解码为 Unicode

这段代码对我有用,但我不确定它打印了什么,因为我的控制台中没有 utf-8 (Windows :P )。

s = '1101100110000110110110011000001011011000101001111101100010101000'
u = "".join([chr(int(x,2)) for x in [s[i:i+8] 
                           for i in range(0,len(s), 8)
                           ]
            ])
d = u.decode('utf-8')

希望这可以帮助!

于 2013-10-08T19:06:25.813 回答
3
>>> s='1101100110000110110110011000001011011000101001111101100010101000'
>>> print (''.join([chr(int(x,2)) for x in re.split('(........)', s) if x ])).decode('utf-8')
نقاب
>>> 

或者,反过来:

>>> s=u'نقاب'
>>> ''.join(['{:b}'.format(ord(x)) for x in s.encode('utf-8')])
'1101100110000110110110011000001011011000101001111101100010101000'
>>> 
于 2013-10-08T19:07:31.997 回答
1

利用:

def bin2text(s): return "".join([chr(int(s[i:i+8],2)) for i in xrange(0,len(s),8)])


>>> print bin2text("01110100011001010111001101110100")
>>> test
于 2013-10-08T18:59:02.523 回答