Base64 is an encoding standard. Everyone knows that some characters are used for controll or some other purposes and cannot be seen. And when transporting online, visible characters can be more convenient. Base64 is invented to solve this problem.
We take three bytes, and evenly split it to four part,then we use the converting table to convert the four part to four visible characters.
If the bytes at tail are shoter than three, we will add zeros to fill it. And put ‘=’ to show it’s added on purpose. So the number of ‘=’ can only be 0,1 or 2.
To make things easier, we will just give the convertion table in C/C++.The result of encoded string can only contain these characters and ‘=’
static char BASE64_ENCODING_TABLE [64] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // 0 - 9
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', // 10 - 19
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', // 20 - 29
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 30 - 39
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', // 40 - 49
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', // 50 - 59
'8', '9', '+', '/' // 60 - 63
};
Take ABC for example, the Ascii is 65,66,67, which in Hex is 41,42,43,the binary representation is 01000001,01000010,01000011, we will split it to 00 010000, 00 010100,00 001001,00 000011,so the corresponding encoding result is QUJD. Decoding is just the reversed operation.