Zig
Zig
Convert any File to Base64 (and back)
Demonstrates how to get the contents of any file as a base64 string, and then write it back.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const bd = try chilkat.BinData.init();
defer bd.deinit();
// This example will load a PDF and return it as a base64 string.
bd.loadFile("qa_data/pdf/helloWorld.pdf") catch {
std.debug.print("Failed to load file.\n", .{});
return;
};
const b64_str = try bd.getEncoded(alloc, "base64");
std.debug.print("{s}\n", .{b64_str});
// Now write the base64 string back to the binary PDF file:
const bd2 = try chilkat.BinData.init();
defer bd2.deinit();
try bd2.appendEncoded(b64_str, "base64");
try bd2.writeFile("qa_output/helloWorld.pdf");
}