3

I am using Python 2.7 to decode base64 data and I don't understand why base64.b64decode returns a string? How do I get to the binary data that is decoded? I would think base64.b64decode would return a byte array. Here is the link to python docs for base64: http://docs.python.org/2/library/base64.html

Thanks for the help!

1
  • 2
    There is no such thing as a byte type in Python 2. It was introduced in Python 3. You just have the str and unicode types in Python 2. Commented Feb 14, 2014 at 1:48

2 Answers 2

4

In 2.x a bytestring is the binary data, represented in a mostly-printable form. And it doesn't require additional modules for support.

Sign up to request clarification or add additional context in comments.

Comments

4

The bytearray type is used when we want mutable strings or arrays of bytes.

The b64decode() function uses a str type which is for immutable strings or arrays of bytes in Python 2.7.

Besides not needing mutability, the other reason that b64decode() didn't consider returning a bytearray is that b64decode() is much older than bytearrays -- when the only tool in your toolbox is a str, all problems start to look like str problems ;-)

1 Comment

I just started using this module so my lack of experience might explain why this isn't a good question. So here it goes if I take some binary data and encode it to base64 then when I decode it I would think it should be to a byte array. I ran into a problem with some Python code I was working on when I tried to process the data as a string I received several exceptions. So after I decode the data the very next thing I do is turn the result into a byte array. Why would anybody want a string? If that was the case the data wouldn't need to be base64 encoded in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.