From 454b2362ee5e80dd1236535eaaec70d98e0d33ba Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 1 Sep 2018 19:40:05 +0900 Subject: std/fmt/index.zig: #1358 allow bytes to be printed-out as hex; Supports {x} for lowercase and {X} for uppercase; --- std/fmt/index.zig | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'std') diff --git a/std/fmt/index.zig b/std/fmt/index.zig index 6d23eebd0b..a8ea5b4c5a 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -350,6 +350,11 @@ pub fn formatText( comptime var width = 0; if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable); return formatBuf(bytes, width, context, Errors, output); + } else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) { + for (bytes) |c| { + try formatInt(c, 16, fmt[0] == 'X', 0, context, Errors, output); + } + return; } else @compileError("Unknown format character: " ++ []u8{fmt[0]}); } return output(context, bytes); -- cgit v1.2.3 From 7a633f472daba84b03dac18557f411c949f6b875 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sat, 1 Sep 2018 19:53:11 +0900 Subject: std/fmt/index.zig: #1358: test bytes printed-out as hex; --- std/fmt/index.zig | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'std') diff --git a/std/fmt/index.zig b/std/fmt/index.zig index a8ea5b4c5a..8e34bd43c4 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -1276,6 +1276,12 @@ test "fmt.format" { try testFmt("E.Two", "{}", inst); } + //print bytes as hex + { + const some_bytes = "\xCA\xFE\xBA\xBE"; + try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", some_bytes); + try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", some_bytes); + } } fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void { -- cgit v1.2.3