#include<sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>
int8_t check_system(void)
{
uint32_t addr = 0x04030201;
uint8_t *p = (uint8_t *)&addr;
if(*p == 0x01) return 1;
else if(*p == 0x04) return 0;
else
printf("check_system error.\n");
return -1;
}
int main()
{
uint32_t host32byte = 0x04030201;
printf("original 32bit hostbyte=0x%08x\n",host32byte);
uint32_t net32byte = htonl(host32byte);
printf("32bit netbyte=0x%08x\n",net32byte);
uint32_t local32byte = ntohl(net32byte);
printf("32bit hostbyte=0x%08x\n\n",local32byte);
uint16_t host16byte = 0x0201;
printf("original 16bit hostbyte=0x%04x\n",host16byte);
uint16_t net16byte = htons(host16byte);
printf("16bit netbyte=0x%04x\n",net16byte);
uint16_t local16byte = ntohs(net16byte);
printf("16bit hostbyte=0x%04x\n\n",local16byte);
int8_t ch = check_system();
if(1 == ch) printf("little-endian.\n");
else if(0 == ch) printf("big-endian.\n");
else printf("check_system error!\n");
return 0;
}
