-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipConversion.c
More file actions
52 lines (38 loc) · 819 Bytes
/
ipConversion.c
File metadata and controls
52 lines (38 loc) · 819 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 16
int* splitIp(char* ip)
{
int i, j, *arr;
int temp =0;
arr = (int*)malloc( 4* sizeof(int));
for(i=0, j=0; i<strlen(ip); i++){
if(ip[i] == '.'){
arr[j] = temp;
temp = 0;
j++;
}
else{
temp = temp*10 + (ip[i] - 48);
}
}
arr[j] = temp;
return arr;
}
int main()
{
int i;
char ip[MAX];
unsigned int res = 0;
scanf("%s", ip);
int *arr = splitIp(ip);
unsigned int a = arr[0] * pow(2, 24);
unsigned int b = arr[1] * pow(2, 16);
unsigned int c = arr[2] * pow(2, 8);
unsigned int d = arr[3] * pow(2, 0);
res = a + b + c + d;
printf("Result = %u", res);
return 0;
}