首页 > 其他 > 详细

字符串-06. IP地址转换(20)

时间:2015-02-10 21:25:15      阅读:1138      评论:0      收藏:0      [点我收藏+]

一个IP地址是用四个字节(每个字节8个位)的二进制码组成。请将32位二进制码表示的IP地址转换为十进制格式表示的IP地址输出。

输入格式:

输入在一行中给出32位二进制字符串。

输出格式:

在一行中输出十进制格式的IP地址,其由4个十进制数组成(分别对应4个8位的二进制数),中间用“.”分隔开。

输入样例:

11001100100101000001010101110010

输出样例:

204.148.21.114

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 
 8 
 9 using namespace::std; 
10 
11 int main(){
12   
13     char a[33];
14     scanf("%s",a);
15     int b=0,c=0,d=0,e=0;
16     for(int i=0;i<=7;i++)
17     {
18         b=b*2+a[i]-0;
19     }
20     for(int i=8;i<=15;i++)
21     {
22         c=c*2+a[i]-0;
23     }
24     for(int i=16;i<=23;i++)
25     {
26         d=d*2+a[i]-0;
27     }
28     for(int i=24;i<=31;i++)
29     {
30         e=e*2+a[i]-0;
31     }
32     printf("%d.%d.%d.%d",b,c,d,e);
33       return 0;
34 }

 

字符串-06. IP地址转换(20)

原文:http://www.cnblogs.com/ligen/p/4284885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!