[C++]普通IP地址转十进制

 1 #include <iostream> 
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     char *IP;
 7     int ip1, ip2, ip3, ip4;
 8     unsigned long result = 0;
 9     printf("Please input IP: ");
10     scanf("%s", IP);
11     sscanf(IP, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
12     if(ip1 > 255 || ip2 > 255 || ip3 > 255 || ip4 > 255)
13     {
14         printf("Input IP is wrong!\n");
15         return 0;
16     }
17     result = ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
18     printf("The decimal IP is: %u.\n", result);
19     return 0;
20 }
21  

 

posted @ 2018-06-11 22:51  hoy0a1d  阅读(685)  评论(0编辑  收藏  举报