-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.c
53 lines (50 loc) · 1.11 KB
/
bin.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
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
static int hex(const int c)
{
switch(c)
{
case '0': return 0x0;
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0x4;
case '5': return 0x5;
case '6': return 0x6;
case '7': return 0x7;
case '8': return 0x8;
case '9': return 0x9;
case 'A': return 0xA;
case 'B': return 0xB;
case 'C': return 0xC;
case 'D': return 0xD;
case 'E': return 0xE;
case 'F': return 0xF;
default:
fprintf(stderr, "unknown char '%c' during conversion\n", c);
exit(1);
}
}
static int bin(const int lo, const int hi)
{
return (hex(lo) << 4) | hex(hi);
}
int main(int argc, char* argv[])
{
if(argc != 3)
{
fprintf(stderr, "expected input and output arguments");
exit(1);
}
FILE* const fi = fopen(argv[1], "r");
FILE* const fo = fopen(argv[2], "w");
for(int c = fgetc(fi); c != EOF; c = fgetc(fi))
{
if(isspace(c))
continue;
fprintf(fo, "%c", bin(c, fgetc(fi)));
}
fclose(fi);
fclose(fo);
}