将base64编码的字符串转换为二进制

将base64编码的字符串转换为二进制

问题描述:

我正在尝试将base64编码的字符串转换为其二进制形式.基本上"cw ==应该返回01100110000.我尝试了各种模块,但似乎找不到合适的模块.任何人有任何想法吗?

I'm trying to convert a base64 encoded string to its binary form. Basically "cw==" should return 01100110000. I tried various modules but can't seem to find a suitable one. Any one got any ideas?

谢谢! j

您必须先对字符串进行解码

You must first decode your string

from base64 import decodestring

然后您可以使用format(ord(x), "b")生成二进制表示形式.

Then you can use format(ord(x), "b") to produce a binary representation.

my_string = "cw=="
print "".join(format(ord(x), "b") for x in decodestring(my_string))
>> '1110011'