C++字節(jié)順序(bigendian&littleendian)

字號:

“Big Endian”是指低地址存放有效字節(jié)(MSB),而”Little Endian“則是低地址存放最低有效字節(jié)(LSB)。PowerPC系列采用big endian方式存儲數(shù)據(jù),而x86系列則采用little endian方式存儲數(shù)據(jù)。
    在計算機中存儲IP地址和端口號是按照主機字節(jié)(host byte)表示的??荚嚧?但是在網(wǎng)絡上指定IP地址和端口號,是按照網(wǎng)絡字節(jié)(network byte)來表示的(就是big endian)。
    u_long htonl(u_long hostlong) 主機字節(jié)轉(zhuǎn)換為網(wǎng)絡字節(jié),32比特整數(shù)
    u_short htons(u_short hostshort)主機字節(jié)轉(zhuǎn)換為網(wǎng)絡字節(jié),16比特整數(shù)
    u_long ntohl(u_long netlong)網(wǎng)絡字節(jié)轉(zhuǎn)換為主機字節(jié),32比特整數(shù)
    u_short ntohs(u_short netshort) 網(wǎng)絡字節(jié)轉(zhuǎn)換為主機字節(jié),16比特整數(shù)