Sem 5‎ > ‎DCCN LAB‎ > ‎

P-1 WAP to translate dotted decimal IP address into 32 bit address.

posted Nov 6, 2012, 5:36 AM by Neil Mathew

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
 
using namespace std;
 
void DEC2BIN(int dec)
{
        // 128 = 2^7 = 8 bit
        //  64 = 2^6 = 7 bit
        //  32 = 2^5 = 6 bit
        //  16 = 2^4 = 5 bit
        //  08 = 2^3 = 4 bit
        //  04 = 2^2 = 3 bit
        //  02 = 2^1 = 2 bit
        //  01 = 2^0 = 1 bit  
 
  for (int i = 128; i != 0; i=i>>1)
  {
    if (dec & i)
      cout<<"1";
    else
      cout<<"0";
  }
 
}
 
 
int main()
        {
 
        //clrscr();
 
        //DECLARATIONS
        int i,j;
        int dec[4];
        int bin[8]={128,64,32,16,8,4,2,1};
 
        //INPUT
        cout<<"Enter the IP Address: ";
        for(i=0;i<4;i++)
        cin>>dec[i];
 
        //TRANSLATION
        cout<<"The ip address is: "<<dec[0];
        cout<<"."<<dec[1]<<"."<<dec[2]<<"."<<dec[3]<<endl;
 
        for(i=0; i<4; i++)
                {
                DEC2BIN(dec[i]);        
 
                if(i!=3)
                cout<<".";
                }
 
        //getch();
        return 1;
        }
 



OUTPUT:

Enter the IP Address: 100 125 0 8
The ip address is: 100.125.0.8
01100100.01111101.00000000.00001000

Comments