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.