Base64 encoding is a very useful technique that allows for the sending of binary data, such as an image file, over the wire using plain-text. This means it can be embedded in XML (plain, SOAP or XML-RPC), it also means the binary data can be stored very easily in a database. It works by reading in data in packs of 6 bits, and turning each one of those into a character in a simple 64 character alphabet. This input data can be text, but it can also be binary data stored in a ByteArray; perhaps the contents of a file, or an MP3 your Flash application has constructed in memory.

Here’s a quick tip that might save you some time and bother. If you are doing any Base64 encoding in ActionScript (for example when sending email attachments or uploading images to a blog over XML-RPC) make sure you split your string up into lines of 76 characters (as is defined in the MIME spec), and add an extra line break at the end. Some Base64 decoders do not require this, but in a lot of common cases for Base64 it is required.

There are already several implementations out there of Base64 encoders/decoders for AS2 and AS3, I was under the impression there was an mx.utils.Base64Encoder class in the Flex but I was not able to find it. This tool is very handy for comparing your results. It also gives the option to upload and convert a file, and specify how many characters to use per line (76 being the common case).