header pic goes here
log in for personalized pages!



Tutor Section: How to translate into base64 and back

Return to base64 Javascript tutorial
Return to tutor home

So we know that using 16 different characters to represent each half byte is a viable option, but not our ideal option because it is only half as space efficient as a byte. So how else can we dice bytes up to get our goal: readable characters for any value of 0 to 255?

Instead of looking at one byte at a time, and trying to chop that byte up, lets take several bytes and see what we can do with them.
As we can easily see, using three bytes, we have a total of 24 bits. How else can we chop 24 bit up? If instead of 3 bytes of 8 bits each we use 4 "clumps" of 6 bytes each, what are we left with? Now we have 26 which equals 64. So now instead of needing 3 instances of a character that can represent any of 256 different combinations, we now need just 4 instances of a character that can represent any of 64 different combinations. The same bits as in the above table fit into the table below.
Now we have to ask ourselves, "do we have 64 readable characters?". The answer is yes. The characters we will use are uppercase A-Z (26 characters), lowercase a-z (26 characters), 0-9 (10 characters), '+' (1 character) and '/' (1 character). 26 + 26 + 10 + 1 + 1 = 64, just the number we need. As you can surmise, base64 is still less space efficient than using a full byte, but instead of hex's double space usage, base64 uses only one and a third as much space. In other words for every 3 bytes, you must have 4 base64 characters. All of the characters listed above are easily readable.

Continue to the mechanics of base64.