Why the inconsistent of those two base64 methods?

Mensanator mensanator at aol.com
Wed May 12 13:12:06 EDT 2010


On May 12, 4:20 am, Maarten <maarten.sn... at knmi.nl> wrote:
> On May 12, 6:04 am, Leo Jay <python.leo... at gmail.com> wrote:
>
> > I'd like to encode a string in base64, but I found a inconsistent of
> > two methods:
>
> > >>> 'aaa'.encode('base64')
> > 'YWFh\n'
> > >>> import base64
> > >>> base64.b64encode('aaa')
> > 'YWFh'
>
> > as you can see, the result of
> > 'aaa'.encode('base64')
> > has a '\n' at the end, but the other method doesn't.
>
> > Why the inconsistent?
>
> Don't know. Does it matter?

Yeah...

>>> import base64
>>> a = 'aaa'.encode('base64')
>>> aa = base64.b64encode('aaa')
>>> a == aa
False


>
> >>> import base64
> >>> base64.decodestring(base64.b64encode('aaa'))
>
> 'aaa'
>
> >>> 'aaa'.encode('base64').decode('base64')
>
> 'aaa'
>
> (so far so good, and as expected)
>
> >>> base64.decodestring('aaa'.encode('base64'))
>
> 'aaa'
>
> >>> base64.b64encode('aaa').decode('base64')
>
> 'aaa'
>
> (As far as I can see, both methods are completely consistent)

Your notion of 'consistency' is wanting.

>
> Maarten




More information about the Python-list mailing list