My task is to convert a data file from big endian to little endian & vice versa using C. I have been looking online for about 3 hours now for other examples and reading my text book, however I am so stuck on how to even start this function.
So far I have the order of events correct (1 through 4) but inside my convert_and_save
function do I have to create a char array using → char buffer[4];
?
Can someone please help me? even if you just give me clues on what to look up, I would greatly appreciate it.
I need to write a function called:
void convert_and_save(struct record item, FILE * output_handle, int number);
inside this function I do the following series of steps:
Convert the integer into an array of chars using:
int integer_to_characters(int number, char * buffer) { memcpy(buffer, &number, 4); }
Reverse the order of the chars in that array.
Convert the array of chars back to an integer using:
int characters_to_integer(char * buffer) { int result; memcpy(&result, buffer, 4); return result; }
write the converted record on the output file using:
void save_record(FILE * file_handle, struct record a) { char output_buffer[size_of_record]; integer_to_characters(a.age, &(output_buffer[0])); memcpy(&(output_buffer[4]), a.name, 12); integer_to_characters(a.department, &(output_buffer[16])); fwrite(output_buffer, sizeof(char), size_of_record, file_handle); }