| ... | ... | @@ -1342,9 +1342,9 @@ pub const hex_charset = "0123456789abcdef"; |
| 1342 | 1342 | |
| 1343 | 1343 | /// Converts an unsigned integer of any multiple of u8 to an array of lowercase |
| 1344 | 1344 | /// hex bytes, little endian. |
| 1345 | | pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 { |
| 1345 | pub fn hex(x: anytype) [@typeInfo(@TypeOf(x)).int.bits / 4]u8 { |
| 1346 | 1346 | comptime assert(@typeInfo(@TypeOf(x)).int.signedness == .unsigned); |
| 1347 | | var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined; |
| 1347 | var result: [@typeInfo(@TypeOf(x)).int.bits / 4]u8 = undefined; |
| 1348 | 1348 | var i: usize = 0; |
| 1349 | 1349 | while (i < result.len / 2) : (i += 1) { |
| 1350 | 1350 | const byte: u8 = @truncate(x >> @intCast(8 * i)); |
| ... | ... | @@ -1360,6 +1360,11 @@ test hex { |
| 1360 | 1360 | try std.testing.expect(x.len == 8); |
| 1361 | 1361 | try std.testing.expectEqualStrings("efbeadde", &x); |
| 1362 | 1362 | } |
| 1363 | { |
| 1364 | const s = "[" ++ hex(@as(u48, 0x12345678_abcd)) ++ "]"; |
| 1365 | try std.testing.expect(s.len == 14); |
| 1366 | try std.testing.expectEqualStrings("[cdab78563412]", s); |
| 1367 | } |
| 1363 | 1368 | { |
| 1364 | 1369 | const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]"; |
| 1365 | 1370 | try std.testing.expect(s.len == 18); |