const std = @import("std");
pub fn main() void {
const foo: u8 = 128;
std.debug.print("{}\n", .{foo}); // "128"
}
Run Code Online (Sandbox Code Playgroud)
上面的打印内容128符合预期。
如何将该值打印为十六进制?
将x或X放在大括号内。
还有其他选项:
const std = @import("std");
pub fn main() void {
// Tested with Zig v0.10.1
const foo: u8 = 26;
std.debug.print("0x{x}\n", .{foo}); // "0x1a"
std.debug.print("0x{X}\n", .{foo}); // "0x1A"
const bar: u16 = 1;
std.debug.print("0x{x}\n", .{bar}); // "0x1"
std.debug.print("0x{x:2}\n", .{bar}); // "0x 1"
std.debug.print("0x{x:4}\n", .{bar}); // "0x 1"
std.debug.print("0x{x:0>4}\n", .{bar}); // "0x0001"
const baz: u16 = 43;
std.debug.print("0x{x:0>4}\n", .{baz}); // "0x002b"
std.debug.print("0x{X:0>8}\n", .{baz}); // "0x0000002B"
const qux: u32 = 0x1a2b3c;
std.debug.print("0x{X:0>2}\n", .{qux}); // "0x1A2B3C" (not cut off)
std.debug.print("0x{x:0>8}\n", .{qux}); // "0x001a2b3c"
}
Run Code Online (Sandbox Code Playgroud)
您可以在https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig阅读有关占位符的更多信息