C Example: Base64 Codec: 3-byte blocks and padding rules

🗓 Last updated: 2026-11-30👤 Written by: Harper✓ Reviewed by: MOTO🏷 Category: Encoding & Conversion

A from-scratch standard Base64 codec: packs every 3 bytes into 24 bits, splits them into four 6-bit indexes, and pads with = when fewer than 3 bytes remain. The decoder merges the indexes back and restores the exact byte count; three inputs verify the round trip.

⚡ Intermediate Reference entries: fn-strlenfn-strchrkw-char
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
text    : Hello, C!
encoded : SGVsbG8sIEMh (12 chars)
decoded : Hello, C!
match   : OK

text    : web-tutorial.com
encoded : d2ViLXR1dG9yaWFsLmNvbQ== (24 chars)
decoded : web-tutorial.com
match   : OK

text    : A
encoded : QQ== (4 chars)
decoded : A
match   : OK
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏