| ... | ... | @@ -1331,3 +1331,23 @@ pub fn isWhiteSpace(byte: u8) bool { |
| 1331 | 1331 | else => false, |
| 1332 | 1332 | }; |
| 1333 | 1333 | } |
| 1334 | |
| 1335 | // depends on https://github.com/ziglang/zig/pull/1454 |
| 1336 | pub 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 | |
| 1347 | test "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 | |