Why Is Base64 Encoding Used?
This is one of the techniques employed by the MIME standard to send data other than plain text.
It does that in essentially two steps.

Wutthichai Luemuang / EyeEm / Getty Images
The first step is to break the binary string down into 6-bit blocks.
None of the special characters available in ASCII are used.
There is also a 65th character known as apad, which is the Equal sign (=).
This character is used when the last segment of binary data doesn’t contain a full 6 bits.
Base64 Encoding Example
For example, take three ASCII numbers 155, 162, and 233.
These three numbers constitute a binary stream of 100110111010001011101001.
A Base64 encoder starts by chunking the binary stream into groupings of six characters: 100110 111010 001011 101001.
Each of these groupings translates into the numbers 38, 58, 11, and 41.
Here’s another way to look at it.
Finally, these numbers are converted to ASCII characters using the Base64 encoding table.
The 6-bit values of this example translate to the ASCII sequencem6Lp.
Using the Base64 conversion table:
This two-step process is applied to the entire binary string that’s encoded.
The newline characters are encoded like all other data.
Base64 Encoding Table
The following table translates all 64 characters used in Base64 encoding.
Solving the Endgame
At the end of the encoding process, there might be a problem.
If the size of the original data in bytes is a multiple of three, everything works fine.
If it is not, there may be empty bytes.
For proper encoding, exactly 3-bytes of binary data is needed.
The solution is to append enough bytes with a value of 0 to create a 3-byte group.
Of course, these artificial trailing ‘0’s cannot be encoded using the encoding table below.
They must be represented by a 65th character.