Base64 Converter
Knowledge points
Base64 encoding principle
Base64 uses 64 characters, A-Z, a-z, 0-9, +, and /, to encode text. The basic idea is to split every three 8-bit bytes into four 6-bit groups, then map each group to the Base64 index table.
- a: 0110 0001
- b: 0110 0010
- 2: 0011 0010
Base64 index mapping
- Y: 0110 00
- W: 0101 10
- I: 0010 00
- y: 1100 10
Before grouping: 0110 0001, 0110 0010, 0011 0010, which are a, b, and 2.
After grouping: 0110 00, 0101 10, 0010 00, 1100 10, mapped to Y, W, I, and y.
So the Base64 encoding of ab2 is YWIy.
Why is Base64 output about 4/3 the original size?
A byte needs 8 bits, but Base64 groups data into 6-bit units. Each 6-bit unit must be padded back to 8 bits for storage.
After padding, 24 original bits become 32 bits, so Base64 output is about 4/3 the size of the original data.
Difference between Base64url and Base64
In standard Base64, + and / are encoded as %2B and %2F inside URLs, which makes URLs longer. Base64url uses A-Z, a-z, 0-9, -, and _ to make encoded text URL-friendly.
