| ... | @@ -2,11 +2,23 @@ const std = @import("std"); | ... | @@ -2,11 +2,23 @@ const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const rawIntBytes = extras.rawIntBytes; | 4 | const rawIntBytes = extras.rawIntBytes; |
| | 5 | const digits = "0123456789abcdef"; |
| | 6 | |
| | 7 | const alphabet = blk: { |
| | 8 | var data: [512]u8 = @splat('0'); |
| | 9 | for (0..16) |m| { |
| | 10 | for (0..16) |n| { |
| | 11 | data[(m * 16 + n) * 2 + 0] = digits[m]; |
| | 12 | data[(m * 16 + n) * 2 + 1] = digits[n]; |
| | 13 | } |
| | 14 | } |
| | 15 | const result = data; |
| | 16 | break :blk result; |
| | 17 | }; |
| 5 | | 18 | |
| 6 | pub fn to_hex(array: anytype) [array.len * 2]u8 { | 19 | pub fn to_hex(array: anytype) [array.len * 2]u8 { |
| 7 | var res: [array.len * 2]u8 = undefined; | 20 | var res: [array.len * 2]u8 = undefined; |
| 8 | var fbs = std.io.fixedBufferStream(&res); | 21 | for (&array, 0..) |x, i| res[i * 2 ..][0..2].* = alphabet[@as(usize, x) * 2 ..][0..2].*; |
| 9 | std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable; | | |
| 10 | return res; | 22 | return res; |
| 11 | } | 23 | } |
| 12 | | 24 | |