htonl() htons() ntohl() ntohs()
네트워크 바이트 순서로 변환
unsigned long htonl(unsigned long hostlong);
//host to network long
unsigned long htons(unsigned long hostshort);
//host to network short
호스트 바이트 순서로 변환
unsigned long ntohl(unsigned long netlong);
//network to host long
unsigned short ntohs(unsigned short netshort);
//network to host short
성공시 : 빅 엔디안 또는 리틀 엔디안 방식으로 변환된 값
빅 엔디안 vs 리틀 엔디안
바이트를 실제 메모리에 기록하는 순서.
0x1234의 표현방식
빅엔디안 0x12 0x34 순으로, 우리가 표현하는 것과 동일하게 표현됨.
즉 가장 큰 수를 담당하는 최상위비트가 제일 왼쪽에 온다.
리틀엔디안 0x34 0x12 순으로, 우리가 표현하는 것과 반대로 표현됨.
즉 가장 작은 수를 담당하는 최하위비트가 제일 왼쪽에 온다.
표준은 없고, 시스템마다 차이를 보인다.
현재 사용하는 시스템이 리틀엔디안인지 빅엔디안인지를 가려주는 코드
int endian;
char *ptr;
endian = 0x12345678;
ptr = (int*)&endian;
printf("Big Endian = 0x12|0x34|0x56|0x78\n");
printf("Little Endian = 0x78|0x56|0x34|0x12\n");
printf("Your System's Byte Order\n");
printf("= 0x%x|0x%x|0x%x|0x%x\n", ptr[0], ptr[1], ptr[2], ptr[3]);
[출처] htonl() htons() ntohl() ntohs()|작성자 에스이오케이