Print Bytes

Prints an array of bytes for debugging purposes

zig 2025-11-09 debug

Description

Prints an array of bytes for debugging purposes. Useful for debugging networking and general (de)serialization purposes.

Example output:

Length: 28
00000000  0d 25 1f 17 05 01 34 23 0d 0c 48 65 6c 6c 6f 00 |.%....4#..Hello.|
00000010  eb 17 1d 9a 57 6f 72 6c 64 21 11 0d EOF         |....World!..|

Code

RAW
pub fn printBytes(bytes: []const u8) void {    std.debug.print("Length: {}", .{bytes.len});    for (0..bytes.len / 16 + 1) |row| {        std.debug.print("\n{x:0>8}  ", .{row * 16});        const end = @min(bytes.len, row * 16 + 16);        const section = bytes[row * 16 .. end];        for (section) |b| {            std.debug.print("{x:0>2} ", .{b});        }        const remaining = 16 - section.len;        if (remaining > 0) {            std.debug.print("EOF", .{});            for (0..remaining - 1) |_| {                std.debug.print("   ", .{});            }        }        std.debug.print("|", .{});        for (section) |b| {            if (b == ' ' or (std.ascii.isPrint(b) and !std.ascii.isWhitespace(b))) {                std.debug.print("{c}", .{b});            } else {                std.debug.print(".", .{});            }        }        std.debug.print("|", .{});    }    std.debug.print("\n", .{});}

Comments

Mode admin 2025-12-27 10:21
comment deleted
Mode admin 2025-12-27 10:22
comment deleted