Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
116 words
1 minutes
240301_zig_base64
link
1. Base64 Math Formula
Base64 encodes 3 bytes → 4 characters.
Mathematical Relationship
Bit Transformation
- 3 bytes (24 bits):
[aaaaaaaabbbbbbbbcccccccc]- Split into 6-bit groups:
[aaaaaa][aabbbb][bbbbcc][cccccc]- Each 6-bit value maps to one Base64 alphabet character.
zig code(ver 0.16)
const std = @import("std");
fn base64_encode(allocator: std.mem.Allocator, input: []const u8) ![]const u8 {
const alphabet_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+"[0..64].*;
var encoder = std.base64.Base64Encoder.init(alphabet_chars, '=');
const size = encoder.calcSize(input.len);
const buf = try allocator.alloc(u8, size);
const result = encoder.encode(buf, input);
return result;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const result = try base64_encode(allocator, "h");
std.debug.print("h __result : {s}\n", .{result});
const result02 = try base64_encode(allocator, "hello");
std.debug.print("hello ___result02 : {s}\n", .{result02});
}240301_zig_base64
https://younghakim7.github.io/blog/posts/240301_zig_base64/