Base64 Encoding of Binary Data
Understand how Base64 encodes binary data into ASCII text. Learn the 6-bit grouping process, the 64-character alphabet, padding rules, and common use cases.
Detailed Explanation
Base64 encoding converts arbitrary binary data into a text string using 64 printable ASCII characters. It exists because many transport protocols (email, JSON, URLs) only reliably handle text, not raw binary bytes.
The Base64 alphabet:
A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), / (63), with = used for padding.
How Base64 works at the bit level:
- Take 3 bytes of input (24 bits total)
- Split into 4 groups of 6 bits each
- Map each 6-bit value to a character from the Base64 alphabet
Step-by-step example — encoding "Hi":
- ASCII values:
H=72,i=105 - Binary:
01001000 01101001 - Add zero-padding to make a multiple of 6 bits:
01001000 01101001 00000000 - Split into 6-bit groups:
010010000110100100000000 - Convert to Base64 indices: 18, 6, 36, 0
- Look up characters:
S,G,k,A - Since input was 2 bytes (not 3), add one
=padding character
Result: "SGk="
Why 6 bits?
The number 64 = 2⁶, so 6 bits can represent values 0-63, mapping perfectly to the 64-character alphabet. Three input bytes (24 bits) divide evenly into four 6-bit groups, which is why Base64 processes data in 3-byte chunks.
The 33% size increase:
Base64 converts every 3 bytes of input into 4 characters of output, a 33% size increase. This is the unavoidable cost of representing binary data as text.
Padding rules:
- 3 input bytes -> 4 Base64 characters (no padding)
- 2 input bytes -> 3 Base64 characters +
= - 1 input byte -> 2 Base64 characters +
==
URL-safe Base64: Replaces + with - and / with _ to avoid conflicts with URL special characters. Used in JWTs, data URIs, and URL parameters.
Use Case
Backend developers use Base64 encoding to embed binary assets like images, certificates, or cryptographic keys within JSON API responses, configuration files, and environment variables.
Try It — Number Base Converter
Related Topics
Convert ASCII Characters to Binary
ASCII Text → Binary (7/8-bit)
Convert Binary to Hexadecimal
Binary (Base 2) → Hexadecimal (Base 16)
Convert Hexadecimal to Decimal
Hexadecimal (Base 16) → Decimal (Base 10)
Unicode Code Points and Binary Encoding
Unicode Characters → Binary (UTF-8/UTF-16)
How to Read a Hexdump
Raw Binary Data → Hexadecimal + ASCII