看板 b97902HW 關於我們 聯絡資訊
單班的同學上許永真老師的計概課的時候, 應該有聽到 Endianness。 它是什麼呢? 簡單講就是大於 1 byte 的整數型別要怎麼在記憶體中表示(Layout)。 例如: 0x04030201 這一個數可以表示成 Address Value 0x00001111: 0x01 0x00001112: 0x02 0x00001113: 0x03 0x00001114: 0x04 也可以表示成 Address Value 0x00001111 0x04 0x00001112 0x03 0x00001113 0x02 0x00001114 0x01 前者是 Little Endian 後者是 Big Endian。 看不懂?沒有關係,大一除了考筆試之外,應該不會碰到這一個問 題。最後附上一段我隨手寫得程式碼,讓大家玩玩,不過請新手不 要學,這裡有不太好的技巧。 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int test[5]; test[1] = 1313821696; test[3] = 5525326; memset(&test[0], '\0', sizeof(int)); memset(&test[2], '\0', sizeof(int)); memset(&test[4], '\0', sizeof(int)); printf("This platform is %s\tlittle endian.\n", test + 1); printf("This platform is %s\tbig endian.\n", test + 3); return EXIT_SUCCESS; } 請在 INT = 4 byte 而且使用 ASCII 的環境之中,編譯、執行。 結果應該很有趣! XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.241.166 ※ 編輯: LoganChien 來自: 140.112.241.166 (10/09 13:52) ※ 編輯: LoganChien 來自: 140.112.241.166 (10/09 14:03)
anfranion:是永真老師唷XD 10/09 14:54
LoganChien:我錯了,偷偷改...... 10/09 14:56
※ 編輯: LoganChien 來自: 140.112.241.166 (10/09 14:56)
xflash96:4e4f5400 v.s. 00544f4e 強. but why memset not '='? 10/10 00:00
drazi:對~ 所以因應不同平台 winsock 專門有函式來處理 10/10 02:14
drazi:這兩種的轉換XD 10/10 02:14