/** 2008/12/04_____________ 學號: 姓名: _____________ _________________ (1) 請在你的電腦上測試這程式寫出其答案 (或任何 Windows 機器) (2) 請找你的直屬學長或同學的直屬學長找到 sun 機器的 gcc 測試 (請注意, 一定要 Sun 的機器, 不是 PC 跑 Solaris) 例如 ccsun15.cc.nctu.edu.tw (學校計中的機器) 把 Sun 機器用 gcc 測試的結果也寫出來做比較; 也請學長教你 在任何 Intel-PC 的 UNIX 機器測試, 比較其結果 ! 例如系上的 bsd2.cs.nctu.edu.tw 或是 linux2.cs.nctu.edu.tw (3) 寫出你的研究心得 (至少 88 字, 至多 200字) (4) 寫一個函數 char* c2hex(char c); which will return a string contains 2 hex digits of the internal value of c; 例如若 c=6; c2hex(c) 傳回 "06"; 若 c=11; c2hex(c) 傳回 "0B"; 若 c=18; c2hex(c)傳回 "12"; 若 c=-1; c2hex(c) 傳回 "FF"; 請確保傳回的字串剛好有16進位兩位數. ********* Hint: Little Endian vs. Big Endian *************/ //program can be found here: www.csie.nctu.edu.tw/~tsaiwn/introcs/OTHERS/ //byteod.c --- byte ordering and union / struct #include typedef union { short haha; char gg[4]; long hehehe; float real; } Egg; void dump(long x) { int i; for(i=1; i<=32; ++i) { if(x<0) printf("1"); else printf("0"); if(i%4==0) printf(" "); x = x << 1; } // for } // dump void printEgg(Egg x) { int i; printf("x.gg[ ] = "); for(i=0; i<4; ++i) printf("%3d ",x.gg[i]); printf(" = 小寫x %%2.2hhx hex "); for(i=0; i<4; ++i) printf("%2.2hhx ",x.gg[i]); // 小寫 %2.2hhx printf("\n = = = = = = = 大寫X %%2.2hhX HEX "); //DEV-CPP 可能不認識 hh for(i=0; i<4; ++i) printf("%2.2hhX ",x.gg[i]); // 大寫 %2.2hhX printf("\n"); // %llx %lx %hx %hhx 在 UNIX 的 gcc/g++ 沒問題 printf("x.haha = %hd == HEX %4.4hX\n", x.haha, x.haha); printf("x.hehehe = %ld == %8.8lX\n", x.hehehe, x.hehehe); //注意%x是把資料當整數 printf("x.real = %15.7f\n", x.real); printf("== binary: "); dump(x.hehehe); printf("\n"); return; } Egg yy; int main( ) { int i; for(i=0; i<4; ++i) yy.gg[i] = (i + 1)%4; printEgg(yy); /// printf("\n=== === Now test 1024+11.375 = 1035.375\n"); yy.real = 1035.375; printEgg(yy); return 0; } /*** Ans: (請依序作答, 不夠寫就寫到背面) ***/