-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasc2bin.c
74 lines (65 loc) · 1.78 KB
/
asc2bin.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
uint8_t getNibble(char ascii) {
uint8_t nibble = 0;
if (ascii >= '0' && ascii <= '9') {
nibble = (uint8_t)(ascii - '0');
} else if (toupper(ascii) >= 'A' || toupper(ascii) <= 'F') {
nibble = (uint8_t)(toupper(ascii) - 'A') + 0xA;
}
return nibble;
}
void main(int argc, char **argv) {
FILE *ifp;
FILE *ofp;
int address;
char idata[1003]; // Max 1000 ASCII characters + end of line and null terminator
uint8_t odata;
int i;
uint8_t nibble;
if (argc < 3) {
printf("Usage: %s [source ASCII file] [converted binary file] [default load address]\n", argv[0]);
return;
}
ifp = fopen(argv[1], "r");
if (ifp == NULL) {
printf("Error opening %s for reading\n", argv[1]);
return;
}
ofp = fopen(argv[2], "w");
if (ofp == NULL) {
printf("Error opening %s for writing\n", argv[2]);
return;
}
if (argc >= 4) {
sscanf(argv[3],"%x",&address);
} else {
// set default load address to 0x0000
address = 0x0000;
}
odata = (uint8_t) (address & 0x00FF);
fwrite(&odata,1,1,ofp);
odata = (uint8_t) ((address & 0xFF00) >> 8);
fwrite(&odata,1,1,ofp);
while (!feof(ifp)) {
if (fgets(idata, 1001, ifp) != NULL) {
if (idata[0] != '#') {
i = 0;
while ((idata[i] >= ' ') && (idata[i] != '#')) {
if (idata[i] == ' ') {
i++;
} else {
nibble = getNibble(idata[i]);
odata = nibble << 4;
nibble = getNibble(idata[i+1]);
odata = odata | nibble;
fwrite(&odata,1,1,ofp);
i += 2;
}
}
}
}
}
}