我写了一个程序将数字写入二进制文件,代码片段如下:
u_int16_t N=150;
u_int16_t seed=3;
FILE * outfile, *infile;
outfile=fopen("tempfile","wb");
//write these 2 16-bit numbers into binary file
fwrite(&seed, 2, 1, outfile);
fwrite(&N, 2, 1, outfile);
infile=fopen("tempfile","rb");
if(infile==NULL) fputs("Fire error\n",stderr);
//get the size of the file
fseek(infile,0,SEEK_END);
int lsize=ftell(infile);
rewind(infile);
u_char * temp2=(u_char*)malloc(lsize);
if(temp2==NULL) printf("temp2 error allocation\n");
fread(temp2,1,lsize,infile);
for(i=0;i<lsize;i++)
printf("%x",temp2[i]);
printf("\n");
fclose(infile);
free(temp2);
结果是:
30960
所以 3 打印为30
小端,而 150 打印为960
,有一个附加0
,实际上0x96=150
,它是大端
为什么和的字节序不同3
,150
为什么还有一个额外的0
?谢谢!