-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspr.c
51 lines (40 loc) · 847 Bytes
/
spr.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
#include <stdio.h>
#include <string.h>
#define BUFSZ 256
char *itoa(char *buf, int val, int base)
{
int i = 30;
for (; val && i; val /= base)
buf[i--] = "0123456789abcdef"[val % base];
return &buf[i + 1];
}
unsigned hatod(char *ptr)
{
const char *hex = "0123456789abcdef";
return (strchr(hex, ptr[0]) - hex) << 4 | (strchr(hex, ptr[1]) - hex);
}
int main(void)
{
char buf[BUFSZ];
char bin[8 + 1];
int n = 0;
//printf("0xfa == %d, %d\n", 0xfa, hatod("fa"));
while (fgets(buf, BUFSZ, stdin)) {
char *s = strstr(buf, ".byte");
if (!s)
continue;
for (unsigned char *ptr = s; *ptr; ++ptr) {
if (*ptr == '$') {
unsigned v = hatod(ptr + 1);
for (int i = 7; i >= 0; --i)
putchar(((v >> i) & 1) ? '1' : '0');
if (++n == 3) {
n = 0;
putchar('\n');
}
}
}
}
putchar('\n');
return 0;
}