2013-03-29 112 views
0

我想通過反轉字節將我的小字節轉換成大字節,但得到的字節與小字節相同。我已經修改了我的代碼,但值仍然相同。如果有人能告訴我我哪裏出錯了。感謝您的幫助通過反轉字節將小字節轉換爲大字節

#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; 
} 
+1

你沒有用'temp'做任何事情。 –

+1

考慮使用ntohl(),htonl()等。它們在任何字節順序的平臺上「做正確的事情」,所以它是可移植的。 –

+0

@RandyHoward,看起來非常有幫助,謝謝,但遺憾的是我不能用這個任務按照教師的指示。 – Mani

回答

1

您更改這裏的字節順序

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

但之後不會再使用temp

您可能要輸出temp中的內容,而不是​​中的內容。

+0

你們是GENIUS ..... !! – Mani

+0

剛剛經歷過;-) –