-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryip.c
101 lines (87 loc) · 2.61 KB
/
binaryip.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <string.h>
int binary_to_decimal(char binaryip[]);
int main(){
char ip[35];
int subnetA[4]={255,0,0,0};
int subnetB[4]={255,255,0,0};
int subnetC[4]={255,255,255,0};
char bin1[9];
int netid[4];
int decimalip[4];
int count=0;
printf("Enter the Ip address in binary format:\n");
scanf("%s",ip);
for(int i=0;i<8;i++){
bin1[i]=ip[i];
}
//creating seperate strings
char newstring[9];
for(int i=0;i<35;i++){
if(ip[i]=='.'){
int decimal=binary_to_decimal(newstring);
decimalip[count]=decimal;
newstring[0]='\0';
count+=1;
continue;
}
char fragstr[2];
sprintf(fragstr,"%c",ip[i]);
strcat(newstring,fragstr);
}
if(bin1[0]=='0'){
printf("This is class A Ip address:\n");
for(int i=0;i<4;i++){
int currnetid=subnetA[i]&decimalip[i];
netid[i]=currnetid;
}
printf("Net id is %d %d %d %d\n",netid[0],netid[1],netid[2],netid[3]);
}else if(bin1[0]=='1'&& bin1[1]=='0'){
printf("This is class B Ip address:\n");
//netid
for(int i=0;i<4;i++){
int currnetid=subnetB[i]&decimalip[i];
netid[i]=currnetid;
}
printf("Net id is %d %d %d %d\n",netid[0],netid[1],netid[2],netid[3]);
}else if(bin1[0]=='1'&& bin1[1]=='1' && bin1[2]=='0'){
printf("This is class C Ip address:\n");
for(int i=0;i<4;i++){
int currnetid=subnetC[i]&decimalip[i];
netid[i]=currnetid;
}
printf("Net id is %d %d %d %d\n",netid[0],netid[1],netid[2],netid[3]);
}else if(bin1[0]=='1'&& bin1[1]=='1' && bin1[2]=='1' && bin1[3]=='0'){
printf("This is class D Ip address:\n");
printf("Class D has no netId");
}else if(bin1[0]=='1'&& bin1[1]=='1' && bin1[2]=='1' && bin1[3]=='1'){
printf("This is class E Ip address:\n");
printf("Class E has No NetId:\n");
}
}
//converting binary into decimal
int binary_to_decimal(char binaryip[]){
int sum=0;
int pow=7;
for(int i=0;i<=7;i++){
int powerproduct=1;
int decimalvalue;
int asciivalue=(int)binaryip[i];
if(asciivalue==48){
decimalvalue=0;
}else if(asciivalue==49){
decimalvalue=1;
}
if(pow==0){
powerproduct=1;
}else{
for(int j=1; j<=pow; j++){
powerproduct*=2;
}
}
int calc=powerproduct*decimalvalue;
sum+=calc;
pow-=1;
}
return sum;
}