authorgravatar for kris.tate+github@gmail.comkristopher tate <kris.tate+github@gmail.com> 2018-09-02 18:02:24+09:00
committergravatar for kris.tate+github@gmail.comkristopher tate <kris.tate+github@gmail.com> 2018-09-03 01:12:52+09:00
logfbd9bac5e7166a9b4ceb56ecb3a659abf6076311
treed72e1e976e78386640723f9a92f298c373a9a92a
parent86e55567b4b1cccbb69065396391e4a500864dce

std/fmt/index.zig: add hexToBytes function under std.fmt;

Depends on #1454 being implemented;

1 files changed, 20 insertions(+), 0 deletions(-)

std/fmt/index.zig+20
...@@ -1331,3 +1331,23 @@ pub fn isWhiteSpace(byte: u8) bool {...@@ -1331,3 +1331,23 @@ pub fn isWhiteSpace(byte: u8) bool {
1331 else => false,1331 else => false,
1332 };1332 };
1333}1333}
1334
1335// depends on https://github.com/ziglang/zig/pull/1454
1336pub fn hexToBytes(input: []const u8, out: []u8) !void {
1337 if (out.len * 2 < input.len)
1338 return error.InvalidLength;
1339
1340 var i: usize = 0;
1341 while (i < input.len) : (i += 2) {
1342 out[i/2] = (try charToDigit(input[i], 36)) << 4;
1343 out[i/2] += try charToDigit(input[i+1], 36);
1344 }
1345}
1346
1347test "fmt.hexToBytes" {
1348 const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
1349 var pb: [32]u8 = undefined;
1350 try hexToBytes(test_hex_str, pb[0..]);
1351 try testFmt(test_hex_str, "{X}", pb);
1352}
1353