首页 > 代码库 > IP Address和Integer相互转换

IP Address和Integer相互转换


#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
void print_ip(int ip)
{
unsigned char bytes[4];
bytes[0] = ip & 0xFF;
bytes[1] = (ip >> 8) & 0xFF;
bytes[2] = (ip >> 16) & 0xFF;
bytes[3] = (ip >> 24) & 0xFF;
printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]);
}

int main(){
//print_ip(3232237185);

printf("%u\n",ntohl( inet_addr("192.168.6.129") ));
//printf("%u\n",inet_addr("192.168.6.129"));

printf("Over");
getchar();
return 0;
}

IP Address和Integer相互转换