summaryrefslogtreecommitdiff
path: root/hex2bin.c
diff options
context:
space:
mode:
Diffstat (limited to 'hex2bin.c')
-rw-r--r--hex2bin.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/hex2bin.c b/hex2bin.c
new file mode 100644
index 0000000..78efd60
--- /dev/null
+++ b/hex2bin.c
@@ -0,0 +1,28 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <ctype.h>
+
+int main() {
+ char c;
+ int n = 0;
+ int count = 0;
+ int skipLine = 0;
+ while ((c = getchar()) != EOF) {
+ if (skipLine && c != '\n') continue;
+ if (isxdigit(c)) {
+ ++count;
+ n <<= 4;
+ if (isdigit(c)) {
+ n |= c - '0';
+ } else {
+ n += 10 + tolower(c) - 'a';
+ }
+ }
+ if (count >= 2 || (count && !isxdigit(c))) {
+ putchar(n);
+ n = count = 0;
+ }
+ skipLine = c == '#';
+ }
+ return 0;
+}