summaryrefslogtreecommitdiff
path: root/hex2bin.c
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
committerPeter Wu <lekensteyn@gmail.com>2012-09-26 15:04:18 +0200
commit2497c1392b81e093f4de2541a98746aebd449a7f (patch)
treedb87e59d4c70c677ddb1899d9161b004f6e454ac /hex2bin.c
downloadc-files-2497c1392b81e093f4de2541a98746aebd449a7f.tar.gz
Initial commit
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;
+}