Endian判斷

References :http://en.wikipedia.org/wiki/Endianness

不懂Endian的話請看上面的參考網站,以下簡單的程式碼可以來判斷
你的電腦是用Little-endian或是Big-endian :

  1 #include
  2 #include
  3 #include
  4
  5 int main(int argc,char * argv[])
  6 {
  7     unsigned char buf[4];
  8     int n = 0xdeadbeef;
  9
 10     memcpy(buf,&n,sizeof(n));
 11     printf("2. %02x %02x %02x %02x\n",buf[0],buf[1],buf[2],buf[3]);
 12     return 0;
 13 }

                                   
如果輸出是ef be ad de 就是 Little-endian,反之輸出de ad be ef就是Big-endian。

留言

熱門文章