The ability to convert data from one format to another has some utility in that one format may be easier to handle, either that it could be considered safer to handle or quite simply, it just just easier to manipulate. For instance, raw binary data may not be suitable to be used in certain locations. Specifically, some binary values may be interpreted in a way that is not desired. Some examples are carriage returns signifying end of an input line, HTML opening tag character less-than (ie: "<"), cookie values containing a simi-colon (ie: ";"), URLs containing an ampersand (ie: "&") or percent (ie: "%") or an email message body containing a JPEG image. In each of these cases, it possible to convert the primary data format into base64 and then place the converted data in the specific location. Of course, this means that the data in that specific location may no longer be used directly and requires being processed on the receiving side, before it may be used.
This experiment really isn't all that large, in fact, the class was written pretty much in a single session by implementing a base64 conversion algorithm as described in the wikipedia base64 page.
Obtain the source via the download page.
Although the in-package documentation is slim, the class is pretty simple and straight forward. The code was written and tested on Linux using g++ 4.1.2. There is a Makefile that should be sufficient to make the class and a simple test program. Simply ungzip and untar the distribution, cd into the src directory and type "make". The program "testBase64" is created that is an example of how to use the Base64 class and also serves as a simple test to make sure the conversion is working as it should. In v2 and later, the code below is also in exampleBase64.cpp and compiles to "exampleBase64" to be run.
#include <iostream>
#include "Base64.h"
int
main(int argc, char *argv[])
{
Base64 b64;
std::string s;
std::string s1;
std::string s2;
s = "Some test.";
std::cout << "Original (" << s << ")\n";
s1 = b64.encode(s,48);
std::cout << "Base64 Encoded(" << s1 << ")\n";
s2 = b64.decode(s1);
std::cout << "Base64 Decoded(" << s2 << ")\n";
}
The "48" in "b64.encode(s,48)" is where to wrap the lines by inserting a newline (ie: every 48 characters, in this example).