1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const rawIntBytes = extras.rawIntBytes;
5const digits = "0123456789abcdef";
6
7const 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};
18
19pub fn to_hex(array: anytype) [array.len * 2]u8 {
20 var res: [array.len * 2]u8 = undefined;
21 for (&array, 0..) |x, i| res[i * 2 ..][0..2].* = alphabet[@as(usize, x) * 2 ..][0..2].*;
22 return res;
23}
24
25pub fn to_HEX(array: anytype) [array.len * 2]u8 {
26 var res: [array.len * 2]u8 = undefined;
27 for (&array, 0..) |x, i| res[i * 2 ..][0..2].* = alphabet[@as(usize, x) * 2 ..][0..2].*;
28 for (&res) |*x| x.* = std.ascii.toUpper(x.*);
29 return res;
30}
31
32test {
33 try std.testing.expect(std.mem.eql(u8, &to_hex(rawIntBytes(u64, 0x4e5a7da9f3f1d132)), "4e5a7da9f3f1d132"));
34}
35test {
36 try std.testing.expect(std.mem.eql(u8, &to_hex(rawIntBytes(u64, 0x4e5a7da9f3f1d132)), "4E5A7dA9F3F1D132"));
37}