在python中将二进制转换为utf-8

Aid*_*n.T 5 python string binary converter utf-8

我有这样的二进制文件: 1101100110000110110110011000001011011000101001111101100010101000

我想将它转换为utf-8.我怎么能在python中做到这一点?

Igo*_*ato 10

清洁版:

>>> test_string = '1101100110000110110110011000001011011000101001111101100010101000'
>>> print ('%x' % int(test_string, 2)).decode('hex').decode('utf-8')
????
Run Code Online (Sandbox Code Playgroud)

反向(来自@Robᵩ的评论):

>>> '{:b}'.format(int(u'????'.encode('utf-8').encode('hex'), 16))
1: '1101100110000110110110011000001011011000101001111101100010101000'
Run Code Online (Sandbox Code Playgroud)

  • 反之亦然:`s = u'نقاب'; print'{:b}'.format(int(s.encode('utf-8').encode('hex'),16))` (2认同)