Skip to content

Instantly share code, notes, and snippets.

@hcccc
Created September 5, 2013 19:52
Show Gist options
  • Save hcccc/6455266 to your computer and use it in GitHub Desktop.
Save hcccc/6455266 to your computer and use it in GitHub Desktop.
Simple helper in C decodes IP address to binary format
#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