-
Notifications
You must be signed in to change notification settings - Fork 10
/
unhex.c
50 lines (43 loc) · 871 Bytes
/
unhex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Hex Decoder -- 2012 Zephyr <[email protected]> This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <stdio.h>
int
main(int argc, char *argv[])
{
unsigned char acc = 0;
unsigned char nybble = 0;
unsigned long int count = 0;
while (1) {
int c = getchar();
count += 1;
switch (c) {
case EOF:
return 0;
case '0' ... '9':
acc = (acc << 4) + c - '0';
nybble += 1;
break;
case 'a' ... 'f':
acc = (acc << 4) + c - 'a' + 10;
nybble += 1;
break;
case 'A' ... 'F':
acc = (acc << 4) + c - 'A' + 10;
nybble += 1;
break;
default:
if (nybble != 0) {
fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count);
}
break;
}
if (nybble == 2) {
putchar(acc);
acc = 0;
nybble = 0;
}
}
return 0;
}