Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

hex

Converts an unsigned integer of any multiple of u8 to an array of lowercase hex bytes, little endian.

fmt.hex
pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8

File

lib/std/fmt.zig:1335

Code

pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8 {
    comptime assert(@typeInfo(@TypeOf(x)).int.signedness == .unsigned);
    var result: [@typeInfo(@TypeOf(x)).int.bits / 4]u8 = undefined;
    var i: usize = 0;
    while (i < result.len / 2) : (i += 1) {
        const byte: u8 = @truncate(x >> @intCast(8 * i));
        result[i * 2 + 0] = hex_charset[byte >> 4];
        result[i * 2 + 1] = hex_charset[byte & 15];
    }
    return result;
}