authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-21 00:15:58 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-21 00:15:58 -08:00
log5be076d136ee82ec02fe12278ae182cba8482b72
tree64ccb9794380a0bd3dd4b83c01f35a4a57bb0a02
parent815c8536f4e9e50e3ae58b191bd6d4bcd09f8796

add from_hex


2 files changed, 19 insertions(+), 0 deletions(-)

src/from_hex.zig created+18
...@@ -0,0 +1,18 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const parseDigits = extras.parseDigits;
5
6pub fn from_hex(array: anytype) [array.len / 2]u8 {
7 var result: [array.len / 2]u8 = undefined;
8 for (0..array.len / 2) |i| result[i] = parseDigits(u8, array[i * 2 ..][0..2], 16) catch unreachable;
9 return result;
10}
11
12test {
13 try std.testing.expectEqualSlices(
14 u8,
15 &.{ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 },
16 &from_hex("3132333435363738393031323334353637383930"),
17 );
18}
src/lib.zig+1
...@@ -119,3 +119,4 @@ pub usingnamespace @import("./compareFnField.zig");...@@ -119,3 +119,4 @@ pub usingnamespace @import("./compareFnField.zig");
119pub usingnamespace @import("./compareFnRange.zig");119pub usingnamespace @import("./compareFnRange.zig");
120pub usingnamespace @import("./asciiLower.zig");120pub usingnamespace @import("./asciiLower.zig");
121pub usingnamespace @import("./parseDigits.zig");121pub usingnamespace @import("./parseDigits.zig");
122pub usingnamespace @import("./from_hex.zig");