0

I am trying to convert my little endian to big endian by reversing the bytes but i get the same output as of the little endian. I have modified my code alot but the value remains the same. If someone could tell me where i am going wrong. Thank you for the help

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        FILE* input;
        FILE* output;

        input = fopen(argv[1],"r");
        output = fopen(argv[2],"w");

        int zipcode, population, value;
        int popluationEndian;

        unsigned char *convert =  (unsigned char*) &population;
        unsigned char temp[4];

        int i;
        while(fscanf(input,"%d %d\n",&zipcode, &population)!= EOF)
        {
                for(i = 0; i<4; i++)
                {
                        temp[i] = convert[3-i];
                }
               popluationEndian = fwrite(&population,sizeof(int),1,output);
        }

        fclose(input);                
        fclose(output);
        return 0;
}
4

1 回答 1

1

You change the byte order here

temp[i] = convert[3-i];

but then never use temp again.

You probably want to output what is in temp rather than what is in population.

于 2013-03-29T21:53:09.950 回答