Function dumpHexFallible [src]

Prints a hexadecimal view of the bytes, returning any error that occurs.

Prototype

pub fn dumpHexFallible(bw: *Writer, ttyconf: tty.Config, bytes: []const u8) !void

Parameters

bw: *Writerttyconf: tty.Configbytes: []const u8

Example

test dumpHexFallible { const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 }; var aw: Writer.Allocating = .init(std.testing.allocator); defer aw.deinit(); try dumpHexFallible(&aw.writer, .no_color, bytes); const expected = try std.fmt.allocPrint(std.testing.allocator, \\{x:0>[2]} 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........ \\{x:0>[2]} 01 12 13 ... \\ , .{ @intFromPtr(bytes.ptr), @intFromPtr(bytes.ptr) + 16, @sizeOf(usize) * 2, }); defer std.testing.allocator.free(expected); try std.testing.expectEqualStrings(expected, aw.written()); }

Source

pub fn dumpHexFallible(bw: *Writer, ttyconf: tty.Config, bytes: []const u8) !void { var chunks = mem.window(u8, bytes, 16, 16); while (chunks.next()) |window| { // 1. Print the address. const address = (@intFromPtr(bytes.ptr) + 0x10 * (std.math.divCeil(usize, chunks.index orelse bytes.len, 16) catch unreachable)) - 0x10; try ttyconf.setColor(bw, .dim); // We print the address in lowercase and the bytes in uppercase hexadecimal to distinguish them more. // Also, make sure all lines are aligned by padding the address. try bw.print("{x:0>[1]} ", .{ address, @sizeOf(usize) * 2 }); try ttyconf.setColor(bw, .reset); // 2. Print the bytes. for (window, 0..) |byte, index| { try bw.print("{X:0>2} ", .{byte}); if (index == 7) try bw.writeByte(' '); } try bw.writeByte(' '); if (window.len < 16) { var missing_columns = (16 - window.len) * 3; if (window.len < 8) missing_columns += 1; try bw.splatByteAll(' ', missing_columns); } // 3. Print the characters. for (window) |byte| { if (std.ascii.isPrint(byte)) { try bw.writeByte(byte); } else { // Related: https://github.com/ziglang/zig/issues/7600 if (ttyconf == .windows_api) { try bw.writeByte('.'); continue; } // Let's print some common control codes as graphical Unicode symbols. // We don't want to do this for all control codes because most control codes apart from // the ones that Zig has escape sequences for are likely not very useful to print as symbols. switch (byte) { '\n' => try bw.writeAll("␊"), '\r' => try bw.writeAll("␍"), '\t' => try bw.writeAll("␉"), else => try bw.writeByte('.'), } } } try bw.writeByte('\n'); } }