diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-02 19:08:54 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-02 19:08:54 -0400 |
| commit | 0d8412d9f0afa399fbb9011c9f2f8f13a00a41a2 (patch) | |
| tree | 39c060584d021d3a623d06293334ca3678acf94a /std | |
| parent | ab387bb4c712b257cc2b728a044ad67935dee2dc (diff) | |
| parent | fbd9bac5e7166a9b4ceb56ecb3a659abf6076311 (diff) | |
| download | zig-0d8412d9f0afa399fbb9011c9f2f8f13a00a41a2.tar.gz zig-0d8412d9f0afa399fbb9011c9f2f8f13a00a41a2.zip | |
Merge branch 'std-fmt-hexToBytes' of https://github.com/kristate/zig into kristate-std-fmt-hexToBytes
Diffstat (limited to 'std')
| -rw-r--r-- | std/fmt/index.zig | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig index f9b68d7941..13a0b525d7 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -1331,3 +1331,23 @@ pub fn isWhiteSpace(byte: u8) bool { else => false, }; } + +// depends on https://github.com/ziglang/zig/pull/1454 +pub fn hexToBytes(input: []const u8, out: []u8) !void { + if (out.len * 2 < input.len) + return error.InvalidLength; + + var i: usize = 0; + while (i < input.len) : (i += 2) { + out[i/2] = (try charToDigit(input[i], 36)) << 4; + out[i/2] += try charToDigit(input[i+1], 36); + } +} + +test "fmt.hexToBytes" { + const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706"; + var pb: [32]u8 = undefined; + try hexToBytes(test_hex_str, pb[0..]); + try testFmt(test_hex_str, "{X}", pb); +} + |
