summaryrefslogtreecommitdiff
path: root/int2bin.c
blob: 78125e315b91e49cff2509b64f33d6dc64d2f933 (plain)
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
88
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#define TRUE	1
#define FALSE	0

static const int ToHex(char c)
{
    c=toupper(c);

    if (c>='0' && c<='9')
	return c-'0';

    if (c>='A' && c<='F')
	return c-'A'+10;

    return 0;
}


int main (int argc, char *argv[])
{
    FILE *in, *out;
    char buff[1024];
    int done;
    int tot;

    if (argc!=3)
    {
    	fprintf(stderr,"%s: usage %s source dest\n",argv[0],argv[0]);
	exit(EXIT_FAILURE);
    }

    if (!(in=fopen(argv[1],"r")))
    {
    	fprintf(stderr,"Couldn't open '%s'\n",argv[1]);
	exit(EXIT_FAILURE);
    }

    if (!(out=fopen(argv[2],"wb")))
    {
    	fprintf(stderr,"Couldn't create '%s'\n",argv[2]);
	exit(EXIT_FAILURE);
    }

    done=0;
    tot=0;

    while(!done)
    {
	if (!fgets(buff,sizeof buff,in))
	{
	    printf("Missing EOF record\n");
	    done=TRUE;
	}

	if (!done && buff[0]!=':')
	{
	    printf("Invalid Intel HEX file\n");
	    done=TRUE;
	}

	if (!done && buff[8]=='1')
	{
	    done=TRUE;
	}

	if (!done)
	{
	    int len;
	    int f;

	    len=ToHex(buff[1])<<4|ToHex(buff[2]);

	    for(f=0;f<len;f++)
	    {
		int b;

		b=ToHex(buff[f*2+9])<<4|ToHex(buff[f*2+10]);
		putc(b,out);
	    }
	}
    }

    return EXIT_SUCCESS;
}