summaryrefslogtreecommitdiff
path: root/int2bin.c
diff options
context:
space:
mode:
Diffstat (limited to 'int2bin.c')
-rw-r--r--int2bin.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/int2bin.c b/int2bin.c
new file mode 100644
index 0000000..78125e3
--- /dev/null
+++ b/int2bin.c
@@ -0,0 +1,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;
+}