|
因為現(xiàn)行的計算機都是以八位一個字節(jié)為存儲單位,那么一個16位的整數(shù),也就是C語言中的short,在內(nèi)存中可能有兩種存儲順序big-
endian和litte-endian.考慮一個short整數(shù)0x3132(0x32是低位,0x31是高位),把它賦值給一個short變量,那么它在內(nèi)存中的存儲可 能有如下兩種情況: 大端字節(jié)(Big-endian): short變量地址 0x1000 0x1001 ___________________________________ | | | 0x31 | 0x32 |________________ | ________________ 高位字節(jié)在低位字節(jié)的前面,也就是高位在內(nèi)存地址低的一端.可以這樣記住(大端->高位->在前->正常的邏輯順序) 小端字節(jié)(little-endian): short變量地址 0x1000 0x1001 _____________________________________ | | | 0x32 | 0x31 |________________ | __________________ 低位字節(jié)在高位字節(jié)的前面,也就是低位在內(nèi)存地址低的一端.可以這樣記住(小端->低位->在前->與正常邏輯順序相反) 可以做個實驗 在windows上下如下程序 #include <stdio.h>
#include <assert.h> void main( void ) { short test; FILE* fp; test = 0x3132; //(31ASIIC碼的’1’,32ASIIC碼的’2’) if ((fp = fopen ("c:""test.txt", "wb")) == NULL) assert(0); fwrite(&test, sizeof(short), 1, fp); fclose(fp); } 然后在C盤下打開test.txt文件,可以看見內(nèi)容是21,而test等于0x3132,可以明顯的看出來x86的字節(jié)順序是低位在前.如果我們 把這段同樣的代碼放到(big-endian)的機器上執(zhí)行,那么打出來的文件就是12.這在本機中使用是沒有問題的.但當你把這個文件從一 個big- endian機器復制到一個little-endian機器上時就出現(xiàn)問題了. 如上述例子,我們在big-endian的機器上創(chuàng)建了這個test文件,把其復制到little-endian的機器上再用fread讀到一個 short里 面,我們得到的就不再是0x3132而是0x3231了,這樣讀到的數(shù)據(jù)就是錯誤的,所以在兩個字節(jié)順序不一樣的機器上傳輸數(shù)據(jù)時需要特別 小心字節(jié)順序,理解了字節(jié)順序在可以幫助我們寫出移植行更高的代碼. 正因為有字節(jié)順序的差別,所以在網(wǎng)絡(luò)傳輸?shù)臅r候定義了所有字節(jié)順序相關(guān)的數(shù)據(jù)都使用big-endian,BSD的代碼中定義了四個宏來處 理: #define ntohs(n) //網(wǎng)絡(luò)字節(jié)順序到主機字節(jié)順序 n代表net, h代表host, s代表short
#define htons(n) //主機字節(jié)順序到網(wǎng)絡(luò)字節(jié)順序 n代表net, h代表host, s代表short #define ntohl(n) //網(wǎng)絡(luò)字節(jié)順序到主機字節(jié)順序 n代表net, h代表host, s代表 long #define htonl(n) //主機字節(jié)順序到網(wǎng)絡(luò)字節(jié)順序 n代表net, h代表host, s代表 long 舉例說明下這其中一個宏的實現(xiàn): #define sw16(x) "
((short)( " (((short)(x) & (short)0x00ffU) << 8) | " (((short)(x) & (short)0xff00U) >> 8) )) 這里實現(xiàn)的是一個交換兩個字節(jié)順序.其他幾個宏類似. 我們改寫一下上面的程序 #include <stdio.h>
#include <assert.h> #define sw16(x) " ((short)( " (((short)(x) & (short)0x00ffU) << 8) | " (((short)(x) & (short)0xff00U) >> 8) )) // 因為x86下面是低位在前,需要交換一下變成網(wǎng)絡(luò)字節(jié)順序 #define htons(x) sw16(x) void main( void ) { short test; FILE* fp; test = htons(0x3132); //(31ASIIC碼的’1’,32ASIIC碼的’2’) if ((fp = fopen ("c:""test.txt", "wb")) == NULL) assert(0); fwrite(&test, sizeof(short), 1, fp); fclose(fp); } 如果在高字節(jié)在前的機器上,由于與網(wǎng)絡(luò)字節(jié)順序一致,所以我們什么都不干就可以了,只需要把#define htons(x) sw16(x)宏替 換為 #define htons(x) (x). 一開始我在理解這個問題時,總在想為什么其他數(shù)據(jù)不用交換字節(jié)順序?比如說我們write一塊buffer到文件,最后終于想明白了, 因為都是unsigned char類型一個字節(jié)一個字節(jié)的寫進去,這個順序是固定的,不存在字節(jié)順序的問題. 【用函數(shù)判斷系統(tǒng)是Big Endian還是Little Endian】 bool IsBig_Endian()
//如果字節(jié)序為big-endian,返回true; //反之為 little-endian,返回false { unsigned short test = 0x1122; if(*( (unsigned char*) &test ) == 0x11) return TRUE; else return FALSE; }//IsBig_Endian() 【打印程序?qū)ο蟮淖止?jié)表示】 // 可在不同平臺與硬件架構(gòu)的機器中測試運行這段代碼,理解大端表示和小端表示的不同.
// 這段代碼使用強制類型轉(zhuǎn)換規(guī)避類型系統(tǒng) #incluede <stdio.h> // 假設(shè)每個字節(jié)都是非負整數(shù) typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { for(int i = 0; i < len; i++) printf(" %.2x", start[i]); printf("\n"); } void show_int(int x) { show_bytes((byte_pointer) &x, sizeof(int)); } void show_float(float x) { show_bytes((byte_pointer) &x, sizeof(float)); } // 在使用相同編碼(如ASCII編碼)的系統(tǒng)中,字符串字節(jié)表示得到的結(jié)果一般是相同的.所以文本數(shù)據(jù)比二進制數(shù)據(jù)具有更強的平臺無關(guān)性 void show_string(char *x) { show_bytes((byte_pointer) x, strlen(x)); } void show_pointer(void *x) { show_bytes((byte_pointer) &x, sizeof(void *)); } void test_show_bytes(int val) { int ival = val; float fval = (float)ival; int *pval = &ival; show_int(ival); // 各個機器因為大端表示和小端表示的不同,從而只是字節(jié)順序不同 show_float(fval); // 各個機器因為大端表示和小端表示的不同,從而只是字節(jié)順序不同 show_pointer(pval); // 指針值是與機器相關(guān)的(linux,sun使用4字節(jié)地址, 而alpha使用八字節(jié)地址) } --------------------------------------------- 對于如數(shù)值12345在int型和float型時的編碼表示 |
|
|
來自: 傷心莫哭 > 《VC編程技術(shù)》