I have a file that contains a matrix of many rows and columns. It looks like something below:
fa ff 00 10 00
ee ee 00 00 30
dd d1 00 aa 00
Each entry in the matrix is a hex number of an eight bit value. I would like to read this file into a two dimensional array.
I have two problems:
Using the read method in my code, *it contains an array that has each entry (two characters) of the matrix. How can I pass each entry into a single variable instead of two characters?
When I pass into the single variable, how to convert it from character to hex? I mean "ff" should be converted to 0xff.
Part of my code is below. I can avoid the tokenize function if better methods can be uesd.
char** tokens;
char** it;
while (fgets(line, sizeof(line), file) != NULL){ /* read a line */
tokens = tokenize(line); // split line
for(it=tokens; it && *it; ++it){
printf("%s\n", *it);
free(*it);
} // end for
} // end while
char** tokenize(const char* str){
int count = 0;
int capacity = 10;
char** result = malloc(capacity*sizeof(*result));
const char* e=str;
if (e) do {
const char* s=e;
e=strpbrk(s," ");
if (count >= capacity)
result = realloc(result, (capacity*=2)*sizeof(*result));
result[count++] = e? strndup(s, e-s) : strdup(s);
} while (e && *(++e));
if (count >= capacity)
result = realloc(result, (capacity+=1)*sizeof(*result));
result[count++] = 0;
return result;
}
fa ff 00 10 00as a number, then we will get1078020018176?