MENU-DRIVEN C PROGRAM FOR IP CLASS IDENTIFICATION & CONVERSION OF IP ADRESSES FROM D TO B AND VICE VERSA.
//this code is designed by codenation.
//8:19 11/28/2020
#include<stdio.h>
void dtob();
void btod();
int main()
{
int ch;
while(1)
{
printf("\nthis is program for ip conversion.\n\n");
printf("enter your choice :\n");
printf("1.conversion from decimal to binary.\n2.conversion from binary to decimal.\n3.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
dtob();
break;
case 2:
btod();
break;
case 3:
return 0;
}
}
}
void dtob()
{
int first,second,third,fourth;
int one[8]={0},two[8]={0},three[8]={0},four[8]={0};
int i,j,rem,dividend;
printf("enter the four bytes ip adress without '.' in decimal :\n");
scanf("%d%d%d%d",&first,&second,&third,&fourth);
printf("\nentered ip address is :\n");
printf("%d.%d.%d.%d\n",first,second,third,fourth);
printf("\n");
if((0<=first && first<=255) && (0<=second && second<=255) && (0<=third && third<=255) && (0<=fourth && fourth<=255))
{
if(0<=first && first<128 )
{
printf("the class of ip address is 'A'.\n");
}
if(127<first && first<192)
{
printf("the class of ip address is 'B'.\n");
}
if(191<first && first<224)
{
printf("the class of ip address is 'C'.\n");
}
if(223<first && first<240)
{
printf("the class of ip address is 'D'.\n");
}
if(239<first && first<=255)
{
printf("the class of ip address is 'E'.\n");
}
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
dividend=first;
for(j=7;j>=0;j--)
{
if(dividend==1)
{
one[j]=1;
break;
}
rem=dividend%2;
one[j]=rem;
dividend=(int)dividend/2;
}
case 1:
dividend=second;
for(j=7;j>=0;j--)
{
if(dividend==1)
{
two[j]=1;
break;
}
rem=dividend%2;
two[j]=rem;
dividend=(int)dividend/2;
}
case 2:
dividend=third;
for(j=7;j>=0;j--)
{
if(dividend==1)
{
three[j]=1;
break;
}
rem=dividend%2;
three[j]=rem;
dividend=(int)dividend/2;
}
case 3:
dividend=fourth;
for(j=7;j>=0;j--)
{
if(dividend==1)
{
four[j]=1;
break;
}
rem=dividend%2;
four[j]=rem;
dividend=(int)dividend/2;
}
}
}
printf("\nthe binary ip adress after conversion is :\n");
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
printf("%d%d%d%d%d%d%d%d",one[0],one[1],one[2],one[3],one[4],one[5],one[6],one[7]);
printf(" ");
break;
case 1:
printf("%d%d%d%d%d%d%d%d",two[0],two[1],two[2],two[3],two[4],two[5],two[6],two[7]);
printf(" ");
break;
case 2:
printf("%d%d%d%d%d%d%d%d",three[0],three[1],three[2],three[3],three[4],three[5],three[6],three[7]);
printf(" ");
break;
case 3:
printf("%d%d%d%d%d%d%d%d\n",four[0],four[1],four[2],four[3],four[4],four[5],four[6],four[7]);
printf(" ");
break;
}
}
}
else
{
printf("\nthe entered ip address is wrong.\nplease enter the valid ip address.\n");
}
}
void btod()
{
int i,j,total=0,msb;
int first[8],second[8],third[8],fourth[8];
int one,two,three,four;
printf("enter the four bytes ip adress without '.' or ' ' in binary:\n");
printf("for eg:\n00000000\n10100010\n11011010\n01010000\n");
printf("press enter after every bit.\n");
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
printf("enter 8 bits of %d byte :\n",i+1);
scanf("%d%d%d%d%d%d%d%d",&first[0],&first[1],&first[2],&first[3],&first[4],&first[5],&first[6],&first[7]);
break;
case 1:
printf("enter 8 bits of %d byte :\n",i+1);
scanf("%d%d%d%d%d%d%d%d",&second[0],&second[1],&second[2],&second[3],&second[4],&second[5],&second[6],&second[7]);
break;
case 2:
printf("enter 8 bits of %d byte :\n",i+1);
scanf("%d%d%d%d%d%d%d%d",&third[0],&third[1],&third[2],&third[3],&third[4],&third[5],&third[6],&third[7]);
break;
case 3:
printf("enter 8 bits of %d byte :\n",i+1);
scanf("%d%d%d%d%d%d%d%d",&fourth[0],&fourth[1],&fourth[2],&fourth[3],&fourth[4],&fourth[5],&fourth[6],&fourth[7]);
break;
}
}
printf("entered ip address is :\n");
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
printf("%d%d%d%d%d%d%d%d",first[0],first[1],first[2],first[3],first[4],first[5],first[6],first[7]);
printf(" ");
break;
case 1:
printf("%d%d%d%d%d%d%d%d",second[0],second[1],second[2],second[3],second[4],second[5],second[6],second[7]);
printf(" ");
break;
case 2:
printf("%d%d%d%d%d%d%d%d",third[0],third[1],third[2],third[3],third[4],third[5],third[6],third[7]);
printf(" ");
break;
case 3:
printf("%d%d%d%d%d%d%d%d\n",fourth[0],fourth[1],fourth[2],fourth[3],fourth[4],fourth[5],fourth[6],fourth[7]);
break;
}
}
if(first[0]==0)
{
printf("the class of ip address is 'A'.\n");
}
if(first[0]==1 && first[1]==0)
{
printf("the class of ip address is 'B'.\n");
}
if(first[0]==1 && first[1]==1 && first[2]==0)
{
printf("the class of ip address is 'C'.\n");
}
if(first[0]==1 && first[1]==1 && first[2]==1 && first[3]==0)
{
printf("the class of ip address is 'D'.\n");
}
if(first[0]==1 && first[1]==1 && first[2]==1 && first[3]==1)
{
printf("the class of ip address is 'E'.\n");
}
printf("\n");
for(i=0;i<4;i++)
{
msb=128;
total=0;
switch(i)
{
case 0:
for(j=0;j<8;j++)
{
total+=msb*first[j];
msb=msb/2;
}
one=total;
case 1:
for(j=0;j<8;j++)
{
total+=msb*second[j];
msb=msb/2;
}
two=total;
case 2:
for(j=0;j<8;j++)
{
total+=msb*third[j];
msb=msb/2;
}
three=total;
case 3:
for(j=0;j<8;j++)
{
total+=msb*fourth[j];
msb=msb/2;
}
four=total;
}
}
printf("the ip address after conversion to decimal is :\n");
printf("%d.%d.%d.%d\n",one,two,three,four);
}
//this code is developed by codenation.
Comments
Post a Comment