Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Using base64 to encode/decode data

If you have binary data, you can encode it with
ascii character to store it more safely.
base64 module is an efficient choice to do so.
It uses characters from this set
('=' is used for padding at the end.)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789+/

Here's how to use it.
>>> import base64
>>> base64.encodestring('hello world')
'aGVsbG8gd29ybGQ=\n'
>>> base64.decodestring(_)
'hello world'
>>>

If the text is long, the base64 module will split
the encoded data into multiple lines.
If you don't wan't it to be split. You can use
binascii.b2a_base64 instead of base64.encodestring and
binascii.a2b_base64 instead of base64.decodestring
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS