www.pudn.com > Hex2c.rar > hex2c.c, change:2002-09-16,size:4217b


////////////////////////////////////////////////////////////////////// 
// 
// File:      hex2c.c 
// $Archive: /USB/Util/hex2c/hex2c.c $ 
// 
// Purpose: 
//    Utility to convert Intel Hex flies into C source code 
// 
// Environment: 
// 
// $Author: Tpm $ 
// 
// $History: hex2c.c $            
//   
//  *****************  Version 1  ***************** 
//  User: Tpm          Date: 7/11/00    Time: 9:26a 
//  Created in $/USB/Util/hex2c 
//   
//  *****************  Version 4  ***************** 
//  User: Markm        Date: 2/23/99    Time: 11:49a 
//  Updated in $/EzUsb/Drivers/hex2c 
//   
//  *****************  Version 3  ***************** 
//  User: Markm        Date: 4/10/98    Time: 2:39p 
//  Updated in $/EZUSB/util/hex2c 
//   
//  *****************  Version 2  ***************** 
//  User: Markm        Date: 4/10/98    Time: 9:48a 
//  Updated in $/EZUSB/util/hex2c 
//   
//   
////////////////////////////////////////////////////////////////////// 
#include <windows.h> 
#include <stdio.h> 
#include <string.h> 
 
#define MAX_FILE_NAME 32 
#define MAX_FILE_SIZE (64 * 1024) 
 
#define MAX_INTEL_HEX_RECORD_LENGTH 16 
 
typedef struct _INTEL_HEX_RECORD 
{ 
   BYTE  Length; 
   WORD  Address; 
   BYTE  Type; 
   BYTE  Data[MAX_INTEL_HEX_RECORD_LENGTH]; 
} INTEL_HEX_RECORD, *PINTEL_HEX_RECORD; 
 
void main(int argc,char *argv[]) 
{ 
   char hexfilename[MAX_FILE_NAME]; 
   char cfilename[MAX_FILE_NAME]; 
   char varname[MAX_FILE_NAME]; 
   unsigned char buffer[MAX_FILE_SIZE]; 
   FILE *fp = NULL; 
   int numread = 0; 
 
   printf("hex2c 1.0\n"); 
 
   if (argc < 3) 
   { 
      printf("usage: hex2c <intel_hexfile_name> <c_filename> <var_name>\n\n"); 
      printf("This utility takes an intel hex file as input and generates\n"); 
      printf("c source code representing the hex file.  The source code \n"); 
      printf("declares an array of INTEL_HEX_RECORD structs.  The user\n"); 
      printf("specifies the name of this array using the var_name argument.\n"); 
      return; 
   } 
 
   strcpy(hexfilename, &argv[1][0]); 
   strcpy(cfilename, &argv[2][0]); 
   if (argc > 3) 
      strcpy(varname, &argv[3][0]); 
   else 
      strcpy(varname, "firmware"); 
 
 
   // 
   // read the Intel Hex input file into a buffer 
   // 
   if ((fp = fopen(hexfilename,"rb")) != NULL) 
   { 
	   numread = fread(buffer,sizeof(unsigned char),MAX_FILE_SIZE,fp); 
	   printf ("Read %d bytes\n",numread); 
	   fclose(fp); 
   } 
   else 
   { 
	   printf("error opening input file\n"); 
	   return; 
   } 
 
   // 
   // create the c sourcecode output file 
   // 
   if ((fp = fopen(cfilename,"wt")) != NULL) 
   { 
      int i; 
      int count = 0; 
      unsigned char *ptr = buffer; 
      unsigned char *record; 
      unsigned char data; 
      INTEL_HEX_RECORD  hexrecord; 
 
      fprintf(fp, "INTEL_HEX_RECORD %s[] = {\n",varname); 
 
      record = strtok(ptr,":"); 
 
      while (record) 
      { 
         count++; 
 
         sscanf(record+0,"%2x",&hexrecord.Length); 
         sscanf(record+2,"%4x",&hexrecord.Address); 
         sscanf(record+6,"%2x",&hexrecord.Type); 
 
         fprintf(fp,"   %d,\n",hexrecord.Length); 
         fprintf(fp,"   0x%x,\n",hexrecord.Address); 
         fprintf(fp,"   %d,\n",hexrecord.Type); 
 
         fprintf(fp,"   {"); 
         if (hexrecord.Length > MAX_INTEL_HEX_RECORD_LENGTH) 
         { 
            printf("Error: Hex Record %d exceeds max record length\n",count); 
            break; 
         } 
         for (i=0; i<hexrecord.Length; i++) 
         { 
            unsigned char* myptr = record+8+(2 * i); 
 
            sscanf(record+8+(2 * i),"%2x",&data); 
            fprintf(fp,"0x%02x",data); 
            if (i != (hexrecord.Length - 1)) 
               fprintf(fp,",",data); 
         } 
         if (hexrecord.Length == 0) 
            fprintf(fp,"0"); 
 
 
         fprintf(fp,"}"); 
 
         record = strtok(NULL,":"); 
         if (record) 
            fprintf(fp,",\n"); 
         else 
            fprintf(fp,"\n"); 
      } 
   	fprintf(fp, "};\n"); 
 
      printf("Processed %d Records\n",count); 
      fclose(fp); 
   } 
   else 
   { 
      printf("error opening output file\n"); 
      return; 
   } 
 
}