std/base64
The std/base64 module provides RFC 4648 Base64 encoding and decoding. Encoding converts binary data into an ASCII string; decoding reverses the process, validating the input length and characters.
Overview
- RFC 4648: Uses the standard alphabet
A-Z,a-z,0-9,+,/with=padding. - Round-trip:
decode(encode(x))returns the original bytes. - Validating:
decoderejects invalid lengths and characters viaResult.
Usage
import "std/encoding/base64.zc"
import "std/io.zc"
fn main() {
let data = (u8*)"foobar";
let encoded = Base64::encode(data, strlen(data));
println(f"Encoded: ");
let decoded = Base64::decode(encoded.c_str());
if (decoded.is_ok()) {
println(f"Decoded back to bytes");
}
}
Struct Definition
struct Base64 {}
Methods
| Method | Signature | Description |
|---|---|---|
| encode | encode(data: u8*, len: usize) -> String | Encodes len bytes into a Base64 String. |
| decode | decode(data: char*) -> Result<Vec<u8>> | Decodes a Base64 string into a Vec<u8>. Returns Err on an invalid length or character. |