Function hex [src]

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

Prototype

pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8

Example

test hex { { const x = hex(@as(u32, 0xdeadbeef)); try std.testing.expect(x.len == 8); try std.testing.expectEqualStrings("efbeadde", &x); } { const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]"; try std.testing.expect(s.len == 18); try std.testing.expectEqualStrings("[00efcdab78563412]", s); } }

Source

pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 { comptime assert(@typeInfo(@TypeOf(x)).int.signedness == .unsigned); var result: [@sizeOf(@TypeOf(x)) * 2]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; }