Sample code for 30+ languages & platforms
Zig

Append Encoded Binary Data to StringBuilder

Demonstrates how to append encoded binary data to the contenets of a StringBuilder.

Chilkat Zig Downloads

Zig
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();

    bd.loadFile("qa_data/jpg/starfish.jpg") catch {
        std.debug.print("Failed to load file.\n", .{});
        return;
    };

    // For example, let's say we want construct simple JSON containing the base64 representation of the above JPG file.
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    sb.append("{ \"jpg\": \"") catch {};
    // GetEncodedSb appends the enocded representation of the binary data to the StringBuiler passed in the 2nd arg.
    bd.getEncodedSb("base64", sb) catch {};
    sb.append("\" }") catch {};

    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // Output looks like this:
    //  { "jpg": "/9j/4AAQSkZJRgABAg...rcQ+vo//2Q==" }
}