Base64 encoding is a convenient way to represent binary data as plain text. This allows for transport of said binary data over a medium that is traditionally more text friendly. In the general case, simply storing binary data as text is quite handy sometimes.

The most common ways to accomplish this are either Base64 encoding, or to HEX. Base64 encoding takes 3 bytes of binary data and turns it into 4 bytes of text. HEX encoding takes 1 byte of binary data, and turns it into 2 bytes of text. 3 bytes of binary data would be transformed into 6 bytes with HEX. While the difference between 4 and 6 bytes isn’t that bad, if you multiply that difference millions of times, the savings really become significant. Therefore, Base64 encoding is more efficient in storage/transport than HEX.

So, here’s my shot at a simple Base64 encoding and decoding in Java. I hammered it out in about 15 minutes; hopefully it’s not horrendously bad. There are tons of other tried/tested Base64 encoders out there; this was an academic exercise at best … for now anyway …