32位数值转化为8位IP地址

#define u8 unsigned char
#define u32 unsigned int
#define SWAP32(ipValue) ((u32)(((ipValue) & 0xff) << 24) | \
						(((ipValue) & 0xff00) << 8) | \
						(((ipValue) >> 8)  & 0xff00) | \
						(((ipValue) >> 24) & 0xff) ); \

int main()
{
	u8 ip_addr[4] = {0};
	unsigned long src = 0x93d4860a;
	src = SWAP32(src)
	ip_addr[0] = (u8) ((src >> 24) & 0xff);
	ip_addr[1] = (u8 ) ((src >> 16) & 0xff);
	ip_addr[2] = (u8 ) ((src >> 8) & 0xff);
	ip_addr[3] = (u8 ) (src & 0xff);
	printf("src = %d.%d.%d.%d \n",ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
	unsigned long dst = 0x3ba41d73;
	dst = SWAP32(dst)
	ip_addr[0] = (u8 ) ((dst >> 24) & 0xff);
	ip_addr[1] = (u8 ) ((dst >> 16) & 0xff);
	ip_addr[2] = (u8 ) ((dst >> 8) & 0xff);
	ip_addr[3] = (u8 ) (dst & 0xff);
	printf("dst = %d.%d.%d.%d \n",ip_addr[0],ip_addr[1],ip_addr[2],ip_addr[3]);
}
posted @ 2020-01-03 13:39  穷书者  阅读(562)  评论(0编辑  收藏  举报