Que*_*atl 2 python decode binary-data
我无法找到以下问题的答案:从字符串开始,将其转换为二进制表示形式.你如何在Python中找回原始字符串?
例:
a = 'hi us'
b = ''.join(format(ord(c), '08b') for c in a)
Run Code Online (Sandbox Code Playgroud)
然后 b = 0110100001101001001000000111010101110011
现在我想在Python 2.x中获得'hi us'.例如,该网站完成了任务:http: //string-functions.com/binary-string.aspx
我已经看到了几个Java的答案,但没有运气实现到Python.我也试过b.decode(),但不知道在这种情况下我应该使用哪种编码.
使用此代码:
import binascii
n = int('0110100001101001001000000111010101110011', 2)
binascii.unhexlify('%x' % n)
Run Code Online (Sandbox Code Playgroud)