[Python-3000] base64 - bytes and strings (original) (raw)

Ron Adam rrr at ronadam.com
Mon Jul 30 04:54:04 CEST 2007


Greg Ewing wrote:

Nick Coghlan wrote:

Py3k strings are unicode, so returning a string would mean you just have to encode it again using the ascii codec to get the bytes to put on the wire. I still believe that producing a string is conceptually the right thing to do. The point of base64 is to encode binary data as text, not binary data as binary data. If I ever had a reason to use base64, it would be because I had a "wire" that would accept text but not binary data, e.g. a file open in text mode, or some other text that I wanted to embed it in. Getting bytes in that situation would force me to make an extra conversion.

Not extra, you just need to make sure your binary data is in the correct range of values the text device you are sending to can handle. As long as it is, it should just work. That is the primary purpose of the base64 encoding. Keep in mind you are sending byte "characters", not integers.

So it would work like the following I think, with the application having responsibility of doing the object to bytes conversion and back, instead of the base64 encoder being limited to only strings.

OUTPUT: convert object to bytes -> encode_64 to bytes -> bytes to output

INPUT: bytes from input* -> decode_64 to bytes -> convert bytes to object

 *Reads text "characters" into bytes instance.

By refusing to guess what the object is, we also create an opportunity to manipulate the results or source further in a bytes instance without doing multiple (or needless) conversions to and from strings.

Cheers, Ron



More information about the Python-3000 mailing list