-
Notifications
You must be signed in to change notification settings - Fork 0
/
dehex.c
87 lines (77 loc) · 1.37 KB
/
dehex.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[1024];
int len = 0;
char *p;
int i;
int o;
int o1, o2;
while (NULL != fgets(buf, 1024, stdin))
{
len = strlen(buf);
//fprintf(stderr, "Read %i\n", len);
int c = 0;
int a;
int sc = 0;
o1 = o2 = -1;
// offset correction.
o1 = 0;
a = 0;
for (i = 0; i < len; i++)
{
p = &buf[i];
o = -1;
if (*p >= '0' && *p <= '9')
o = *p - '0';
else if (*p >= 'a' && *p <= 'f')
o = *p - 'a' + 10;
else if (*p >= 'A' && *p <= 'F')
o = *p - 'A' + 10;
else if (*p == ' ' || *p == '\t' || *p == ':')
{
// inc space counter
sc++;
// if we run into more then 2, its garbage.
if (sc > 2)
break;
continue;
}
else
break;
// we must have got a good char, so reset space counter
sc = 0;
if (o >= 0)
{
c++;
if (c > 39)
break;
// which part of the octet are we reading?
if (o1 == -1)
o1 = o;
else
o2 = o;
// Must have a full octet
if (o2 != -1)
{
int n;
a++;
n = (o1 << 4) | o2;
if (a > 4)
{
//printf("%c ", *p);
//printf("%02x %02x ", o1, o2);
//printf("%02x ", n);
printf("%c", n);
}
//printf("%02i %02i ", o1, o2);
o1 = o2 = -1;
}
}
}
//fwrite("\n", sizeof(char), 1, stdout);
}
return 0;
}