線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:7262
推到 Plurk!
推到 Facebook!

Endian system tester

 
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-23 11:08:35 IP:61.231.xxx.xxx 訂閱
靈感來自 lcsboy 的 □【分享】什麼是"Big Endian" & "Little Endian"  http://delphi.ktop.com.tw/topic.php?TOPIC_ID=29224    因為自己也常接觸微處理器,如 8051,DSP,... 所以,寫這個程式來判斷該處理器是屬於何種系統。    程式碼很短,如下:
//---------------------------------------------------------------------------    #pragma hdrstop
#pragma argsused    #include     // source by dllee http://dllee.ktop.com.tw
// idea from lcsboy http://delphi.ktop.com.tw/topic.php?TOPIC_ID=29224    typedef union  {
  long          l;
  unsigned char c[4];
} EndianTest;    int main(int argc, char* argv[])
{
  EndianTest a;
  a.l=0x12345678;      if(a.c[0]==0x78
  && a.c[1]==0x56
  && a.c[2]==0x34
  && a.c[3]==0x12)
  {
    printf("This system is 'Little Endian'.\n");
  }
  else if(a.c[0]==0x12
  && a.c[1]==0x34
  && a.c[2]==0x56
  && a.c[3]==0x78)
  {
    printf("This system is 'Big Endian'.\n");
  }
  else
  {
    printf("This system is 'Unknown Endian'.\n");
  }      printf("for a long variable value is 0x%lX\n",a.l);
  printf("and its storage order in memory :\n");
  for(int i=0;i<4;i  )
    printf("%p : 0xX\n",&a.c[i],a.c[i]);      getchar();  // wait for a key ...
  return 0;
}
//----------------------------------------------------------------------------//
我所用過的微處理器好像都是 'Little Endian',而我也一直認為這是「理所當然」的記憶體配置... 謝謝 lcsboy 的提醒。 註:以上的程式碼,是用一般 ANSI C 所寫,必需拿到您使用的系統(非 x86 的處理器) 重新組譯後,才能由執行的結果判斷該系統是 Big/Little/Unknown Endian System. 沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com
附加檔案:29229_EndianTest.zip
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-23 11:10:45 IP:61.231.xxx.xxx 訂閱
執行結果:
This system is 'Little Endian'.
for a long variable value is 0x12345678
and its storage order in memory :
0012FF88 : 0x78
0012FF89 : 0x56
0012FF8A : 0x34
0012FF8B : 0x12
沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-05-17 17:12:54 IP:59.105.xxx.xxx 訂閱
程式碼重貼 <textarea cols="60" rows="10" class="cpp" name="code"> //--------------------------------------------------------------------------- #pragma hdrstop #pragma argsused #include // source by dllee http://dllee.ktop.com.tw // idea from lcsboy http://delphi.ktop.com.tw/topic.php?TOPIC_ID=29224 typedef union { long l; unsigned char c[4]; } EndianTest; int main(int argc, char* argv[]) { EndianTest a; a.l=0x12345678; if(a.c[0]==0x78 && a.c[1]==0x56 && a.c[2]==0x34 && a.c[3]==0x12) { printf("This system is 'Little Endian'.\n"); } else if(a.c[0]==0x12 && a.c[1]==0x34 && a.c[2]==0x56 && a.c[3]==0x78) { printf("This system is 'Big Endian'.\n"); } else { printf("This system is 'Unknown Endian'.\n"); } printf("for a long variable value is 0x%lX\n",a.l); printf("and its storage order in memory :\n"); for(int i=0;i<4;i ) printf("%p : 0xX\n",&a.c[i],a.c[i]); getchar(); // wait for a key ... return 0; } //----------------------------------------------------------------------------// </textarea>
------
http://www.ViewMove.com
系統時間:2024-04-25 20:40:28
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!