解法一:普通处理
#include<stdio.h>
void PrintIP(unsigned int ip)
{
int i = 0;
int a =0;
for(int i = 0;i<4;++i)
{
a = ip & 0x0ff;
printf("%d.",a);
ip>>= 8;
}
printf("\n");
}
int main()
{
unsigned int ip = 3456765443;PrintIP(ip);// "192.168.23.119";
return 0;
}
解法二:使用union
#include<stdio.h>
union IPNode
{
unsigned int addr;
struct
{
unsigned char s1,s2,s3,s4;
};
};void PrintIP(unsigned int ip)
{
IPNode x;
x.addr = ip;
printf("%d.%d.%d.%d\n",x.s1,x.s2,x.s3,x.s4);
}
int main()
{
unsigned int ip = 3456765443;PrintIP(ip);// "192.168.23.119";
return 0;
}