Base64 implementation in Zig
by Gx Anshu | Feb 2, 2025
My simple and efficient implementation of base64 encoding/decoding written in Zig, featuring both a library and command-line interface.
Features
- Base64 encoding and decoding functionality
- Command-line interface for:
- Encoding text strings
- Encoding files
- Help documentation
- Memory-safe implementation
- Standard base64 alphabet support
Development
- Make sure you have Zig installed on your system
- Clone this repository:
git clone https://github.com/yourusername/base64-zig.git
cd base64-zig
- Build the project:
zig build
Usage
Command Line Interface
The tool provides several commands:
# Encode a text string
base64-zig --text "hello its gxanshu"
# Encode a file
base64-zig --file "/path/to/your/file.pdf"
# Show help
base64-zig -h
# or
base64-zig --help
Library Usage
You can also use the base64 implementation in your Zig projects:
const Base64 = @import("root.zig").Base64;
// Initialize the encoder
const base64 = Base64.init();
// Encode
const encoded = try base64.encode(input_bytes, allocator);
defer allocator.free(encoded);
// Decode
const decoded = try base64.decode(encoded_string, allocator);
defer allocator.free(decoded);
Testing
Run the built-in tests:
zig test src/root.zig
Implementation Details
- Standard base64 alphabet (A-Z, a-z, 0-9, +, /)
- Padding with ‘=’ characters when necessary
- Efficient memory usage with proper allocation and deallocation
- Error handling for invalid input