From 8da140fd5548cfab0a2e945091ec78416b1a0d14 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 19 May 2022 15:45:37 +0200 Subject: lib: introduce hexenc() and hexdec() Add two new functions to deal with encoding and decoding of hexadecimal digit strings: - hexenc() - convert the given input value into a lower case hex digit string, implicitely converting the input argument to a string value if needed - hexdec() - decode the given input hex digit string into a byte string, skipping whitespace or optionally specified characters in the input Signed-off-by: Jo-Philipp Wich --- tests/custom/03_stdlib/56_hexdec | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/custom/03_stdlib/56_hexdec (limited to 'tests/custom/03_stdlib/56_hexdec') diff --git a/tests/custom/03_stdlib/56_hexdec b/tests/custom/03_stdlib/56_hexdec new file mode 100644 index 0000000..cb842ca --- /dev/null +++ b/tests/custom/03_stdlib/56_hexdec @@ -0,0 +1,29 @@ +The `hexdec()` function decodes the given hexadecimal digit string into +a byte string, optionally skipping specified characters. + +Returns null if the input string contains invalid characters or an uneven +amount of hex digits. + +Returns the decoded byte string on success. + +-- Testcase -- +{% + printf("%.J\n", [ + hexdec("44 55 66 77 33 44\n"), // whitespace is skipped by default + hexdec("44-55-66:77-33-44", ":-"), // skip specified characters + hexdec("abc"), // error; uneven amount of digits + hexdec("ab cd !"), // error; non-whitespace, non-hex, non-skipped char + hexdec(1234), // error; non-string input + ]); +%} +-- End -- + +-- Expect stdout -- +[ + "DUfw3D", + "DUfw3D", + null, + null, + null +] +-- End -- -- cgit v1.2.3