How Base64 Works: Complete Visual Explanation
โ Back to Blog
How Base64 Works: Complete Visual Explanation
ยท 5 min read
From Bytes to Bits: A Foundation Review
To understand Base64, you first need to know how computers store data. Computers use binary (0s and 1s) to store all information. 8 bits make 1 byte, and 1 byte can represent values from 0 to 255. Character 'A' has ASCII code 65, corresponding to binary 01000001; 'B' is 66, binary 01000010; 'C' is 67, binary 01000011.
Base64 works by regrouping the binary representation of these bytes โ not in 8-bit groups but in 6-bit groups โ then mapping each 6-bit group to a printable character. This regrouping is the core operation of Base64.
Complete Encoding Example: String "ABC"
Step 1: ่ทๅ ASCII ๅผ / Get ASCII values
A = 65, B = 66, C = 67
Step 2: ่ฝฌๆขไธบ 8 ไฝไบ่ฟๅถ / Convert to 8-bit binary
A = 01000001
B = 01000010
C = 01000011
Step 3: ่ฟๆฅๆๆๆฏ็น / Concatenate all bits
010000 010100 001001 000011
โ โ โ โ
16 20 9 3 โ 6ไฝๅ่ฟๅถๅผ / 6-bit decimal values
Step 4: ๆฅ Base64 ๅญๆฏ่กจ / Look up Base64 alphabet
16 = Q
20 = U
9 = J
3 = D
Step 5: ่พๅบ็ปๆ / Output
"ABC" โ "QUJD"
Note: 3 input bytes (24 bits) produce exactly 4 output characters (4ร6=24 bits), with no information lost or added โ only the grouping method changes.
The Base64 Alphabet (Index Table)
Index Char Index Char Index Char Index Char
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /
(pad) =
Handling Less Than 3 Bytes
็คบไพ๏ผๅชๆ 1 ไธชๅญ่ "M" (77 = 01001101)
Example: Just 1 byte "M" (77 = 01001101)
ไบ่ฟๅถ๏ผ01001101
ๅไธบ 6+2 ไฝ๏ผ010011 | 01xxxx ๏ผไธ่ถณ6ไฝ่กฅ0๏ผ
โ 010011 = 19 โ 'T'
โ 010000 = 16 โ 'Q'
ๅกซๅ
ไธคไธช็ญๅท / Add two padding chars
็ปๆ / Result: "TQ=="
็คบไพ๏ผๅชๆ 2 ไธชๅญ่ "Ma" (77, 97 = 01001101 01100001)
Example: Just 2 bytes "Ma" (77, 97)
ไบ่ฟๅถ๏ผ01001101 01100001
ๅไธบ 6+6+4 ไฝ๏ผ010011 | 010110 | 0001xx
โ 010011 = 19 โ 'T'
โ 010110 = 22 โ 'W'
โ 000100 = 4 โ 'E'
ๅกซๅ
ไธไธช็ญๅท / Add one padding char
็ปๆ / Result: "TWE="
The Decoding Process (Reverse Operation)
Decoding is the exact reverse of encoding. Given a Base64 string, restore it with these steps: (1) remove padding characters (=), noting the count; (2) replace each Base64 character with its 6-bit index value; (3) concatenate all 6-bit blocks into one long bit string; (4) split into 8-bit groups, restoring bytes; (5) based on padding count, remove excess bytes from the end (1 = sign removes 1 byte, 2 = signs remove 2 bytes).
่งฃ็ "QUJD" ็่ฟ็จ / Decoding "QUJD":
Q=16=010000, U=20=010100, J=9=001001, D=3=000011
่ฟๆฅ / Concatenate: 010000 010100 001001 000011
้็ปไธบ 8 ไฝ / Regroup to 8-bit: 01000001 01000010 01000011
่ฝฌๆขไธบ ASCII / Convert to ASCII: A(65) B(66) C(67)
็ปๆ / Result: "ABC"
Why Size Increases by 33%
The mathematical reason for the 33% size increase is straightforward: every 3 bytes (24 bits) of raw data is represented as 4 characters (1 byte each, total 4 bytes). 4/3 โ 1.333, meaning a ~33% increase. MIME-format Base64 (76 characters per line) adds another ~1.5% overhead for line breaks.
This is the inherent efficiency cost of Base64 encoding. If you need more efficient binary-to-text encoding, consider Base85 (Ascii85), which uses 85 characters to represent 4 bytes as 5 characters โ approximately 25% size overhead. But Base85's character set is more complex, with less widespread compatibility than Base64.
Computational Complexity of Base64 Encoding
Base64's time complexity is O(n), where n is the number of input bytes. The encoding process requires only simple bit operations and array table lookups, without any complex computation. On modern hardware, Base64 encoding speed is very fast, typically achieving several GB/s throughput (with SIMD-optimized implementations).
Space complexity is also O(n), with output size ceil(n/3)*4 bytes. Understanding Base64's performance characteristics helps make sound decisions in system design โ for example, whether to convert large binary files to Base64 before storing in a database, or use dedicated binary storage (like BYTEA, BLOB).
Try the online tool now โ no installation, completely free.
Open Tool โ
Try the free tool now
Use Free Tool โ