-
Notifications
You must be signed in to change notification settings - Fork 1
/
convbin.c
48 lines (40 loc) · 991 Bytes
/
convbin.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
void main(int argc, char **argv) {
FILE *ifp;
FILE *ofp;
int address;
uint8_t idata;
uint8_t odata[2];
if (argc < 3) {
printf("Usage: %s [source binary 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[0] = (uint8_t) (address & 0x00FF);
odata[1] = (uint8_t) ((address & 0xFF00) >> 8);
fwrite(odata,1,2,ofp);
while (!feof(ifp)) {
if (fread(&idata,1,1,ifp) > 0) {
fwrite(&idata,1,1,ofp);
}
}
fclose(ifp);
fclose(ofp);
}