Base64 encoding and decoding


A small class implementing base 64 (RFC 2045) encoding and decoding.

Code is based on the original GPLed work by Dawit Alemayehu and Rik Hemsley (KDE development team). Many changes have been made and the routines should be slightly faster now.




Usage example

Base64 decoding:

#include <QByteArray> #include <QBitArray> #include "base64.h"
// ...
// Decode into a byte array
QByteArray ba1 = Base64::decode(myEncodedString1);
/* Convenience method, decode into a bit array (myBitCount contains the number of bits we want to decode) */
QBitArray ba2 = Base64::decode(myEncodedString2, myBitCount);
// ...

Base64 encoding:

#include <QByteArray> #include <QBitArray> #include "base64.h"
// ...
// Don't do this at home kids!
QFile myFile("/dev/random");; QByteArray buffer = myQFile.readAll(); QString b64string = Base64::encode(buffer);
// ...

Everything is static, so you don't need to instantiate any class.

Technical details

About the KDE code

The original Alemayehu and Hemsley version did not ignore padding-chars during decoding (see RFC 2045), this version does.

Line length limit

RFC requires base 64 encoded strings to be no longer than 76 characters. The Base64 class adds a CR/LN pair after 76 chars on a per-request basis (the limitLines parameter in the encodeString() method).