I try to write a struct into file, then i found the endian of that is differet with the endian in the memory.
some test code:
void show_hex(unsigned char *p, int n)
{
for (int i=0; i<n;i++){
printf("%02X ",p[i]);
}
}
int main()
{
FILE *fp = fopen("as","w");
struct X{
int x,y;
};
struct X *p = malloc( sizeof(struct X));
p->x = 0xFFEECCAA;
p->y = 0xFFAADD;
show_hex((unsigned char *) p, sizeof(struct X));
fwrite(p, sizeof(struct X), 1, fp);
fclose(fp);
int f = open("as2",O_WRONLY);
write(f, p, sizeof(struct X));
close(f);
return 0;
}
the problem out put : AA CC EE FF DD AA FF 00 //i know that is the little endian
tyw@um08:~/pro|master⚡ ⇒ hexdump as
0000000 ccaa ffee aadd 00ff
0000008
tyw@um08:~/pro|master⚡ ⇒ hexdump as2
0000000 ccaa ffee aadd 00ff
0000008
So the endin is different.
echo 1234 | hexdumpinstead of assuming the endian was wrong. When all else fails,man hexdump.