Hi Guys,

Basically I have an application that reads in a .hex file and I want to be able to then sort the hex values out line by line into a 2D Array ready for further processes.

Example of a .hex file

:10010000214601360121470136007EFE09D2190140
:100110002146017EB7C20001FF5F16002148011988
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF

Below code is the ReadHexFile() function

void ReadHexFile(char *name)
{
	FILE *file;
	char *buffer;
	unsigned long fileLen;

	//Open file
	file = fopen("Project.hex", "rb");
	if (!file)
	{
		fprintf(stderr, "Unable to open file %s", name);
		return;
	}
	
	//Get file length
	fseek(file, 0, SEEK_END);
	fileLen=ftell(file);
	fseek(file, 0, SEEK_SET);

	//Allocate memory
	buffer=(char *)malloc(fileLen+1);
	if (!buffer)
	{
		fprintf(stderr, "Memory error!");
                                fclose(file);
		return;
	}

	//Read file contents into buffer
	fread(buffer, fileLen, 1, file);
	fclose(file);

	//Sort out buffer into a 'structure' ready to send down the CAN
	sortBuff(buffer);

	free(buffer);
}

The sortBuff() is designed to take the large char array and sort it out into a 2D array, line by line. Here is what I got so far:

void sortBuff(char * input)
{
	int HexArrSort[760][45] = {0};
	int	line = -1, hexChar = 0, int i = 0;
	
	
	
	while(!eof)
	{
		//Start of new line
		if(input[i] == ':')
		{
			line++;
			hexChar = 0;
		
		}
		
		//end of file
		if((input[i] == 'F') && (input[i+1] == 'F'))
		{
			if(input[i+2] == '\0')
			{
				eof = 1; //True
			}
		}
		
		
		HexArrSort[line][hexChar] = input[i];
		i++;
		hexChar++;
	
	}

Does anyone have any sugesstions on a more efficient way of doing this? If you need any more info let me know, cheers.

Edited 5 Years Ago by DesignGhosts: n/a

line 3 of sortBuff(). That array should be declared as char, not int because you are putting alphanumeric characters into it, not integers.

line 19 of sortBuff() -- why is that check even necessary?

while( line[i] != '\0')
{  
    if( line[i] == ':')
    {
       ++line;
       hexChar = 0;
    }
    else if( line[i] != 'F' && line[i+1] != 'F')
    {
        HexArrSort[line][hexChar++] = input[i];
    }
    ++i;        
}

Probably an even more efficient way to do that is to combine the two functions you posted and read each line directly into the array. Just call fgets() to read each line, something like this:

char** buffer = 0;
int numLines = 0;
int linesUsed = 0;
char line[80] = {0};
file = fopen("Project.hex", "rt"); // open in text mode
while( fgets(line, sizeof(line), file) )
{
    line[strlen(line)-1] = 0; // truncate '\n'
    if( linesUsed == numLines )
    {
       numLines += 10; // allocate  10 lines (pointers)
       buffer = realloc(buffer,numLines, sizeof(char*));
    }
    buffer[linesUsed] = strdup(line);
    ++linesUsed;
}

Edited 5 Years Ago by Ancient Dragon: n/a

Ancient Dragon - Much appreciated for the reply!

line 19 of sortBuff() -- why is that check even necessary?

- the check determines the end of the .hex file. All .hex files finish up with 01FF which is end of file record type (01) and checksum (FF).

A question regarding the second source code...what is the strdup(line) function, I haven't come across it...is it ANSI?

Cheers

strdup()

it's the same as this

char* s = malloc(15); // allocate some memory
strcpy(s,"Hello");

>>the check determines the end of the .hex file
Maybe so, but the check is completly unnecessary as I pointed out in my previous post.

Edited 5 Years Ago by Ancient Dragon: n/a

The check is not unnecessary (*), but it is plain wrong. An address field of the EOF record is not necessarily all zeroes, so the checksum is not necessarily FF.

(*) This check helps to determine the integrity of the file. However it only makes sense if every other record is also tested against the data length, checksum, record type, etc. My recommendation is to make all these tests while reading the file, byte by byte.

This article has been dead for over six months. Start a new discussion instead.