Zig
Zig
StringBuilder Encode
Demonstrates the Chilkat StringBuilder Encode method.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 s = "The quick brown fox jumps over the lazy dog";
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
sb.append(s) catch {};
// output: The quick brown fox jumps over the lazy dog
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
sb.encode("base64", "utf-8") catch {};
// output: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
sb.decode("base64", "utf-8") catch {};
// output: The quick brown fox jumps over the lazy dog
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
// Now encode to hex..
sb.encode("hex", "utf-8") catch {};
// output: 54686520717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F67
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
sb.decode("hex", "utf-8") catch {};
// URL encode
sb.encode("url", "utf-8") catch {};
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
sb.decode("url", "utf-8") catch {};
// base64url
sb.encode("base64url", "utf-8") catch {};
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
// other possible encodings include modbase64, base32, base58, quoted-printable, url_rfc1738, url_rfc2396, url_rfc3986, and fingerprint
}