toq*_*eos 10 python string unicode binary
是否有一种简单的方法可以将一些str/unicode对象表示为一个大二进制数(或十六进制数)?
我一直在阅读相关问题的一些答案,但没有一个适用于我的场景.
我尝试使用STL中的struct模块,但它没有按预期工作.Chars,就像在二进制文件中一样,显示为好的字符.
我在尝试一些不可能的事吗
例:
def strbin(inp):
# sorcery!
return out
>> print strbin("hello")
# Any of these is cool (outputs are random keystrokes)
0b1001010101010000111001110001...
0xad9f...
Run Code Online (Sandbox Code Playgroud)
sen*_*rle 17
你可以尝试bitarray
:
>>> import bitarray
>>> b = bitarray.bitarray()
>>> b.fromstring('a')
>>> b
bitarray('01100001')
>>> b.to01()
'01100001'
>>> b.fromstring('pples')
>>> b.tostring()
'apples'
>>> b.to01()
'011000010111000001110000011011000110010101110011'
Run Code Online (Sandbox Code Playgroud)
非常简单,不需要来自pypi的模块:
def strbin(s):
return ''.join(format(ord(i),'0>8b') for i in s)
Run Code Online (Sandbox Code Playgroud)
您需要使用Python 2.6+才能使用它.