authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-02 19:08:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-02 19:08:54-04:00
log0d8412d9f0afa399fbb9011c9f2f8f13a00a41a2
tree39c060584d021d3a623d06293334ca3678acf94a
parentab387bb4c712b257cc2b728a044ad67935dee2dc
parentfbd9bac5e7166a9b4ceb56ecb3a659abf6076311
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'std-fmt-hexToBytes' of https://github.com/kristate/zig into kristate-std-fmt-hexToBytes


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