user3350512
user3350512

Reputation: 1

Convert a base64 encoded string to binary

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?

thanks! j

Upvotes: 0

Views: 5400

Answers (1)

Mp0int
Mp0int

Reputation: 18747

You must first decode your string

from base64 import decodestring

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'

Upvotes: 1

Related Questions