What's Base64 encoding
Base64 encoding is a way to convert any binary or text data into printable ASCII string format. It is called Base64 because it works with a subset of 64 characters from the ASCII character set. These characters include upper and lowercase alphabets A-Z
a-z
, digits 0-9
, +
, and /
. Note that, the Base64 encoding scheme also uses an extra 65th character = as padding character.
Base64 encoding detail
Base64 encoding is multi-step process. But it is very simple. Here is how it works:
Convert the input to 8-bit bytes
Re-group the input to 6-bit groups.
Find the decimal equivalent of the 6-bit groups.
Use the Base64 alphabet table to find the character corresponding to the decimal values
If the input can’t be grouped into an integral number of 6-bit groups, then one or two pad character is added to the output based on the following conditions:
- If the last group contains 2 bits, append 4 zero bits to the input.
- If the last group contains 4 bits, append 2 zero bits to the input.
In the first case, two pad characters (=) are appended to the Base64 output and in the second case, a single pad character (=) is appended.
Let's see an example:
Input: @!
8-bit bytes: 01000000 00100001
6-bit groups: 010000 000010 000100
# (Appended with two zero bits at the end to form an integral no of 6-bit groups)
Decimal values: 16 2 4
Base64: Q C E
Final output: QCE=
# (Appended with one pad character to account for the two zero bits added to the input.)