Skip to content

Commit

Permalink
std/fmt/index.zig: add hexToBytes function under std.fmt;
Browse files Browse the repository at this point in the history
Depends on ziglang#1454 being implemented;
  • Loading branch information
kristate committed Sep 2, 2018
1 parent 86e5556 commit fbd9bac
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions std/fmt/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit fbd9bac

Please sign in to comment.