Function hexToBytes [src]

Decodes the sequence of bytes represented by the specified string of hexadecimal characters. Returns a slice of the output buffer containing the decoded bytes.

Prototype

pub fn hexToBytes(out: []u8, input: []const u8) ![]u8

Parameters

out: []u8input: []const u8

Example

test hexToBytes { var buf: [32]u8 = undefined; try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))}); try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))}); try expectFmt("", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, ""))}); try std.testing.expectError(error.InvalidCharacter, hexToBytes(&buf, "012Z")); try std.testing.expectError(error.InvalidLength, hexToBytes(&buf, "AAA")); try std.testing.expectError(error.NoSpaceLeft, hexToBytes(buf[0..1], "ABAB")); }

Source

pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 { // Expect 0 or n pairs of hexadecimal digits. if (input.len & 1 != 0) return error.InvalidLength; if (out.len * 2 < input.len) return error.NoSpaceLeft; var in_i: usize = 0; while (in_i < input.len) : (in_i += 2) { const hi = try charToDigit(input[in_i], 16); const lo = try charToDigit(input[in_i + 1], 16); out[in_i / 2] = (hi << 4) | lo; } return out[0 .. in_i / 2]; }