⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intelhex.c

📁 Intel Hex Converter, Source
💻 C
字号:
/* -*- Mode: C -*- *//* intelhex.c - Generate an Intel-hex file * Created by Robert Heller on Thu Nov 26 19:57:22 1992 * * ------------------------------------------------------------------ * Modification History: * ------------------------------------------------------------------ * Contents: * ------------------------------------------------------------------ *  *  * Copyright (c) 1992 by Robert heller *        All Rights Reserved *  */#include <stdio.h>#include <errno.h>#include <ctype.h>void writeIntelHex(array,arraysize,base,outfilename)unsigned char array[];  /* data bytes */int arraysize,base;     /* size of array, base address of data */char *outfilename;      /* output filename */{        register int rc,bc;        register unsigned char checksum,byte;        register unsigned short int lineaddress;        FILE *ofp;        static char linebuffer[40];        register char *lp;        int error;        ofp = fopen(outfilename,"w");        if (ofp == NULL)        {                error = errno;                fprintf(stderr,                        "intelhex: writeIntelHex: fopen(%s) failed, errno = %d\n",                        outfilename,error);                exit(error);        }        for (rc = 0; rc < arraysize; rc += 16)        {                checksum = 16;                checksum += (rc & 0x0ff);                checksum += ((rc >> 8) & 0x0ff);                sprintf(linebuffer,"%04x",rc+base);                for (lp = &linebuffer[0],bc=0;bc < 4;lp++,bc++)                {                        if (islower(*lp)) *lp = toupper(*lp);                }                lp = &linebuffer[4];                for (bc = 0; bc < 16; bc++)                {                        byte = array[rc+bc];                        checksum += byte;                        sprintf(lp,"%02x",byte);                        if (islower(*lp)) *lp = toupper(*lp);                        lp++;                        if (islower(*lp)) *lp = toupper(*lp);                        lp++;                }                checksum = ~checksum;                checksum++;                sprintf(lp,"%02x",checksum);                if (islower(*lp)) *lp = toupper(*lp);                lp++;                if (islower(*lp)) *lp = toupper(*lp);                lp++;                *lp = '\0';                printf(":10%s\n",linebuffer);                fprintf(ofp,":10%s\n",linebuffer);        }        printf(":00000000\n");        fprintf(ofp,":00000000\n");        fclose(ofp);}main(argc,argv)int argc;char **argv;{        char *infile, *outfile;        unsigned char *prom;        int arraysize, base;        long int offset;        FILE *ifp;        int error;        extern void * malloc();        infile = NULL;        outfile = NULL;        prom = NULL;        arraysize = 0;        base = 0;        offset = 0l;        argc--; argv++; /* skip program name */        while (argc-- > 0)        {                if (**argv != '-')                {                        if (infile == NULL) infile = *argv++;                        else if (outfile == NULL) outfile = *argv++;                        else usageabort("Too many parameters");                }                else if (strncmp(*argv,"-size=",6) == 0)                {                        if (strlen(*argv) == 6) usageabort("Missing value (-size=)");                        arraysize = atoi((*argv)+6);                        argv++;                }                else if (strncmp(*argv,"-base=",6) == 0)                {                        if (strlen(*argv) == 6) usageabort("Missing value (-base=)");                        sscanf((*argv)+6,"%x",&base);                        argv++;                }                else if (strncmp(*argv,"-offset=",8) == 0)                {                        if (strlen(*argv) == 8) usageabort("Missing value (-offset=)");                        sscanf((*argv)+8,"%lx",&offset);                        argv++;                }                else if (strcmp(*argv,"-?") == 0 ||                         strcmp(*argv,"-help") == 0) usageabort(NULL);                else                {                        static char message[512];                        sprintf(message,"Unknown option: %s",*argv);                        usageabort(message);                }        }        if (infile == NULL) usageabort("Input filename missing");        if (outfile == NULL) usageabort("Output filename missing");        ifp = fopen(infile,"r");        if (ifp == NULL)        {                error = errno;                fprintf(stderr,"intelhex: main: fopen(%s) failed, errno=%d\n",                        infile,error);                exit(error);        }        if (arraysize == 0)        {                /* -size= not given.  do file from offset to eof */                fseek(ifp,0,2);         /* seek eof */                arraysize = ftell(ifp) - offset;        }        prom = (unsigned char *) malloc(arraysize);        if (prom == NULL)        {                error = errno;                fprintf(stderr,                        "intelhex: main: malloc(%d) failed, errno=%d\n",                        arraysize,error);                exit(error);        }        if (fseek(ifp,offset,0) < 0)        {                error = errno;                fprintf(stderr,                        "intelhex: main: fseek(%s,0x%08lx) failed, errno=%d\n",                        infile,offset,error);                exit(error);        }        if (fread(prom,sizeof(unsigned char),arraysize,ifp) < arraysize)        {                error = errno;                fprintf(stderr,                        "intelhex: main: fread(%s,%d) failed, errno=%d\n",                        infile,arraysize,error);                exit(error);        }        fclose(ifp);        writeIntelHex(prom,arraysize,base,outfile);}usageabort(message)char *message;{        if (message != NULL) printf("%s\n\n",message);        else printf("Function: generate an Intel hex file from binary data\n");        printf("Syntax: intelhex options infile outfile options\n");        printf("Options:\n");        printf("\t-help:\t\tThis text\n");        printf("\t-?:\t\tThis text\n");        printf("\t-size=ssss\tNumber of bytes in the output file in decimal\n");        printf("\t-base=bbbb\tPROM base address in hex\n");        printf("\t-offset=oooooo\tFile offset to start from in hex\n");        printf("\n\n");        if (message == NULL) exit(0);        else exit(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -