Categories
Tags
3d ABI Actions algorithms alignment analyze APIT Arc Architecture arm array ascii ash assembly asynchronous attribute base64 bithacks Blogging box c c23 cdylib CHAR_BIT clamp 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 dll doc DP drawio dtruss dylib Dynamic emulator example Example FFI ffmpeg 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 inference intel interop invariant iterator join justfile kernel LaTeX leak LFU limits linux lto MachineLearning macOS map Markdown math ML mmap mod mp3 nc nextest OnceLock optimization OS ownership panic parallels perf physics pin postgresql procedure product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift SIMD sin size SmallProjects so socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous syscall tan test thread time toml tracing traits trap triangulation type uint32_t UnsafeRust utf16 utf8 vector 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/