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


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);
	//Get file length
	fseek(file, 0, SEEK_END);
	fseek(file, 0, SEEK_SET);

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

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

	//Sort out buffer into a 'structure' ready to send down the CAN


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;
		//Start of new line
		if(input[i] == ':')
			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];

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

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] == ':')
       hexChar = 0;
    else if( line[i] != 'F' && line[i+1] != 'F')
        HexArrSort[line][hexChar++] = input[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);

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?



it's the same as this

char* s = malloc(15); // allocate some memory

>>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.

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.