1.思维导图
2.指针形式验证大小端存储
#include<myhead.h>
int main(int argc, const char *argv[])
{
int num = 0x12345678;
char* ptr = (char *)#
if(*ptr == 0x12)
{
printf("big endian\n");
}
else if(*ptr == 0x78)
{
printf("little endian\n");
}
return 0;
}
3.共用体形式验证大小端存储
#include<myhead.h>
int main(int argc, const char *argv[])
{
union Endian
{
int num;
char ch;
}data;
data.num = 0x12345678;
if(data.ch = 0x12)
{
printf("big endian\n");
}
else if(data.ch == 0x78)
{
printf("little endian\n");
}
return 0;
}
4.主机字节序与网络字节序转换
#include<myhead.h>
int main(int argc, const char *argv[])
{
unsigned int num = 0x12345678;
unsigned short value = 0x1234;
unsigned int num1 = htonl(num);
unsigned short value1 = htons(value);
printf("num1 = %#x\n",num1);
printf("value1 = %#x\n",value1);
return 0;
}
5.点分十进制
#include<myhead.h>
int main(int argc, const char *argv[])
{
char *ip = "192.126.125.194";
in_addr_t ip_test = inet_addr(ip);
printf("ip_test = %#x\n",ip_test);
printf("对应的主机字节序为:%#x\n",ntohl(ip_test));
return 0;
}
6.手动实现将小端存储的数据转换成大端存储
#include<myhead.h>
int main(int argc, const char *argv[])
{
int num = 0x12345678;
char *ptr = (char*)#
if(*ptr == 0x12)
{
printf("big endian\n");
}
else if(*ptr == 0x78)
{
printf("little endian\n");
}
char *qtr = ptr+3;
while(qtr > ptr)
{
char temp = *ptr;
*ptr = *qtr;
*qtr = temp;
ptr++;
qtr--;
}
printf("num = %#x\n",num);
return 0;
}