diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 03:10:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 03:10:08 +0000 |
commit | 5262a872f308b3b584c97d621992fb3877e392b8 (patch) | |
tree | b956c322376141abeafe639bd72cfecdf16954b5 /debug/hex2bin.c | |
parent | Initial commit. (diff) | |
download | xz-utils-5262a872f308b3b584c97d621992fb3877e392b8.tar.xz xz-utils-5262a872f308b3b584c97d621992fb3877e392b8.zip |
Adding upstream version 5.6.1+really5.4.5.upstream/5.6.1+really5.4.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'debug/hex2bin.c')
-rw-r--r-- | debug/hex2bin.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/debug/hex2bin.c b/debug/hex2bin.c new file mode 100644 index 0000000..7324624 --- /dev/null +++ b/debug/hex2bin.c @@ -0,0 +1,53 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file hex2bin.c +/// \brief Converts hexadecimal input strings to binary +// +// Author: Lasse Collin +// +// This file has been put into the public domain. +// You can do whatever you want with this file. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "sysdefs.h" +#include <stdio.h> +#include <ctype.h> + + +static int +getbin(int x) +{ + if (x >= '0' && x <= '9') + return x - '0'; + + if (x >= 'A' && x <= 'F') + return x - 'A' + 10; + + return x - 'a' + 10; +} + + +int +main(void) +{ + while (true) { + int byte = getchar(); + if (byte == EOF) + return 0; + if (!isxdigit(byte)) + continue; + + const int digit = getchar(); + if (digit == EOF || !isxdigit(digit)) { + fprintf(stderr, "Invalid input\n"); + return 1; + } + + byte = (getbin(byte) << 4) | getbin(digit); + if (putchar(byte) == EOF) { + perror(NULL); + return 1; + } + } +} |