pta ip地址转换

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

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

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

输入样例:
11001100100101000001010101110010
输出样例:
204.148.21.114

#include
int main()
{
    char a[32];
    int sum1,sum2,sum3,sum4;
        scanf("%s",a);
        if(a[0]-'0' == 1)
        {
            sum1 += 128;
        }
        if(a[1]-'0' == 1)
        {
            sum1 += 64;
        }
        if(a[2]-'0' == 1)
        {
            sum1 += 32;
        }
        if(a[3]-'0' == 1)
        {
            sum1 += 16;
        }
        if(a[4]-'0' == 1)
        {
            sum1 += 8;
        }
        if(a[5]-'0' == 1)
        {
            sum1 += 4;
        }
        if(a[6]-'0' == 1)
        {
            sum1 += 2;
        }
        if(a[7]-'0' == 1)
        {
            sum1 += 1;
        }


        if(a[8]-'0' == 1)
        {
            sum2 += 128;
        }
        if(a[9]-'0' == 1)
        {
            sum2 += 64;
        }
        if(a[10]-'0' == 1)
        {
            sum2 += 32;
        }
        if(a[11]-'0' == 1)
        {
            sum2 += 16;
        }
        if(a[12]-'0' == 1)
        {
            sum2 += 8;
        }
        if(a[13]-'0' == 1)
        {
            sum2 += 4;
        }
        if(a[14]-'0' == 1)
        {
            sum2 += 2;
        }
        if(a[15]-'0' == 1)
        {
            sum2 += 1;
        }


        if(a[16]-'0' == 1)
        {
            sum3 += 128;
        }
        if(a[17]-'0' == 1)
        {
            sum3 += 64;
        }
        if(a[18]-'0' == 1)
        {
            sum3 += 32;
        }
        if(a[19]-'0' == 1)
        {
            sum3 += 16;
        }
        if(a[20]-'0' == 1)
        {
            sum3 += 8;
        }
        if(a[21]-'0' == 1)
        {
            sum3 += 4;
        }
        if(a[22]-'0' == 1)
        {
            sum3 += 2;
        }
        if(a[23]-'0' == 1)
        {
            sum3 += 1;
        }


        if(a[24]-'0' == 1)
        {
            sum4 += 128;
        }
        if(a[25]-'0' == 1)
        {
            sum4 += 64;
        }
        if(a[26]-'0' == 1)
        {
            sum4 += 32;
        }
        if(a[27]-'0' == 1)
        {
            sum4 += 16;
        }
        if(a[28]-'0' == 1)
        {
            sum4 += 8;
        }
        if(a[29]-'0' == 1)
        {
            sum4 += 4;
        }
        if(a[30]-'0' == 1)
        {
            sum4 += 2;
        }
        if(a[31]-'0' == 1)
        {
            sum4 += 1;
        }
        printf("%d.%d.%d.%d",sum1,sum2,sum3,sum4);
    return 0;
}

你可能感兴趣的:(C)