1/* 2 * Main. C 3 * D6-string-06. IP address conversion 4 * created on: August 19, 2014: http://blog.csdn.net/junjieguo/article/details/7392539 7 */8 9 10 # include <stdio. h> 11 12 INT bin_dec (int x, int N) // the User-Defined Function converts the binary number to 10 hexadecimal 13 {14 if (n = 0) 15 {16 return 1; 17} 18 return x * bin_dec (x, n-1); // recursively call the bin_dec () function 19} 20 21 int main (void) 22 {23 int I; 24 int IP [4] = {0}; 25 char a [33]; 26 // printf ("Enter the binary number: \ n "); 27 scanf ("% s", a); 28 for (I = 0; I <8; I ++) 29 {30 if (a [I] = '1') 31 {32 IP [0] + = bin_dec (2, 7-i ); 33} 34} 35 for (I = 8; I <16; I ++) 36 {37 if (a [I] = '1 ') 38 {39 IP [1] + = bin_dec (2, 15-i); 40} 41} 42 for (I = 16; I <24; I ++) 43 {44 if (a [I] = '1') 45 {46 IP [2] + = bin_dec (2, 23-i ); 47} 48} 49 for (I = 24; I <32; I ++) 50 {51 if (a [I] = '1 ') 52 {53 IP [3] + = bin_dec (2, 31-i); 54} 55 if (a [I] = '\ 0') 56 {57 break; 58} 59} 60 // printf ("IP: \ n"); 61 printf ("% d. % d. % d. % d \ n ", IP [0], IP [1], IP [2], IP [3]); // output IP address 62 63 return 0; 64}
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E5%AD%97%E7%AC%A6%E4%B8%B2-06
Reprinted:
Http://blog.csdn.net/junjieguo/article/details/7392539