Sample code for 30+ languages & platforms
Zig

Demonstrate Binary Data Encoding Methods

Demonstrates binary data encoding methods.

Note: This example requires Chilkat v9.5.0.64 or later.

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 bin_data = try chilkat.BinData.init();
    defer bin_data.deinit();

    // Append 10 bytes: 00, 01, 02, 03, 04, 05, 06, 07, 08, 09
    bin_data.appendEncoded("00010203040506070809", "hex") catch {};

    // Show that binData contains 10 bytes.
    std.debug.print("num bytes = {d}\n", .{bin_data.getNumBytes()});

    // Get as base64
    std.debug.print("base64: {s}\n", .{try bin_data.getEncoded(alloc, "base64")});

    // Get a chunk of the binary data.
    // The 1st byte is at index 0.
    // The output should be "02030405"
    var offset: i32 = 2;
    var num_bytes: i32 = 4;
    std.debug.print("chunk: {s}\n", .{try bin_data.getEncodedChunk(alloc, offset, num_bytes, "hex")});

    // Copy the bytes to a StringBuilder using the base64url encoding
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    bin_data.getEncodedSb("base64url", sb) catch {};
    std.debug.print("base64url: {s}\n", .{try sb.getAsString(alloc)});

    // Remove a chunk from the binary data
    offset = 2;
    num_bytes = 4;
    bin_data.removeChunk(offset, num_bytes) catch {};
    // The bytes remaining are 00,01,06,07,08,and 09
    std.debug.print("after removing chunk: {s}\n", .{try bin_data.getEncoded(alloc, "hex")});
}