位元組排序函式與位元組操縱函式

2021-06-11 00:52:39 字數 963 閱讀 3324

考慮乙個16bit的整數,它由2個位元組組成。記憶體中儲存這兩個位元組有兩種方法:一種是將低序位元組儲存在起始位置,這種稱為小端(little-endian)位元組序,另一種是將高序位元組儲存在起始位址,這稱為大端(bin-endian)位元組序。msb(最高有效位):16位數的最左一位。lsb(最低有效位):最右一位。

檢視主機的位元組序:

#include#includeint main()

un; un.s=0x0102;

if(sizeof(short)==2)

else

printf("szieof(short)=%d\n",sizeof(short));

exit(0);

}

網路位元組序採用大端位元組序,以下函式實現網路位元組序與主機位元組序的轉換:

#include

uint32_t htonl(uint32_t hostlong);

uint16_t htons(uint16_t hostshort);

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);

源自4.3bsd套接字提供:

void bzero(void *s, size_t n);

void bcopy(const void *src, void *dest, size_t n);

int bcmp(const void *s1, const void *s2, size_t n);

源自ansc c提供:

void *memset(void *s, int c, size_t n);

void *memcpy(void *dest, const void *src, size_t n);

int memcmp(const void *s1, const void *s2, size_t n);

位元組操縱函式

名字以b開頭的第一組函式起源於4.2bsd,幾乎所有現今支援套接字函式的系統仍然提供他們,名字以men 表示記憶體 開頭的第二組函式起源於ansi c標準。支援ansi c函式庫的所有系統都提供他們。berkeley函式 include void bzero void dest,size t nby...

UNIX網路程式設計讀書筆記 位元組操縱函式

include void bzero void dest,size t nbytes void bcopy const void src,void dest,size t nbytes int bcmp const void ptr1,const void ptr2,size t nbytes 返回...

位元組排序函式

位元組序是指多位元組資料在計算機記憶體中儲存或者網路傳輸時各位元組的儲存順序。consider a 16 bit integer that is made up of 2 bytes.there are two ways to store the twobytes in memory with th...