authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-02 19:25:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-02 19:25:07-04:00
log8558caecb591af94a7e51cd226e7291a9337e76d
tree5ed3117bae6a4f67d067fa2a36888275363228b5
parentab387bb4c712b257cc2b728a044ad67935dee2dc
parent3eb89ee4db31eef9540273b25689c0515da6bf57
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'kristate-std-fmt-hexToBytes'


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

std/fmt/index.zig+20-1
......@@ -350,7 +350,7 @@ pub fn formatText(
350350 comptime var width = 0;
351351 if (fmt.len > 1) width = comptime (parseUnsigned(usize, fmt[1..], 10) catch unreachable);
352352 return formatBuf(bytes, width, context, Errors, output);
353 } else if ((fmt[0] == 'x') or (fmt[0] == 'X') ) {
353 } else if ((fmt[0] == 'x') or (fmt[0] == 'X')) {
354354 for (bytes) |c| {
355355 try formatInt(c, 16, fmt[0] == 'X', 2, context, Errors, output);
356356 }
......@@ -1331,3 +1331,22 @@ pub fn isWhiteSpace(byte: u8) bool {
13311331 else => false,
13321332 };
13331333}
1334
1335pub fn hexToBytes(out: []u8, input: []const u8) !void {
1336 if (out.len * 2 < input.len)
1337 return error.InvalidLength;
1338
1339 var in_i: usize = 0;
1340 while (in_i != input.len) : (in_i += 2) {
1341 const hi = try charToDigit(input[in_i], 16);
1342 const lo = try charToDigit(input[in_i + 1], 16);
1343 out[in_i / 2] = (hi << 4) | lo;
1344 }
1345}
1346
1347test "fmt.hexToBytes" {
1348 const test_hex_str = "909A312BB12ED1F819B3521AC4C1E896F2160507FFC1C8381E3B07BB16BD1706";
1349 var pb: [32]u8 = undefined;
1350 try hexToBytes(pb[0..], test_hex_str);
1351 try testFmt(test_hex_str, "{X}", pb);
1352}