Created
September 5, 2013 19:52
-
-
Save hcccc/6455266 to your computer and use it in GitHub Desktop.
Simple helper in C decodes IP address to binary format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int main(){ | |
long int remainder, quotient; | |
int binaryNumber[100], i = 1, j = 0, k = 0, l = 0; | |
int remaining_zero = 0; | |
int ipaddr[4]; | |
printf("Enter IP: "); | |
scanf("%d.%d.%d.%d", &ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]); | |
for (k = 0; k < 4; k++) { | |
i = 1; j = 0; | |
memset(binaryNumber, 0, sizeof(int)*100); | |
quotient = ipaddr[k]; | |
while (quotient != 0) { | |
binaryNumber[i++] = quotient % 2; | |
quotient = quotient / 2; | |
} | |
remaining_zero = 8 - (i - 1); | |
for (l = 0; l < remaining_zero; l++) | |
printf("0"); | |
for (j = i - 1; j > 0; j--) | |
printf("%d", binaryNumber[j]); | |
if (k != 3) | |
printf("|"); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment