将十进制ip转换为二进制显示

时间:2023-02-13 20:59:09
 

将十进制ip转换为二进制显示。如输入128.1.2.128,转换为10000000.00000001.00000010.10000000

#include <stdio.h>#include <string.h>//将IP地址转化为二进制void bin(int s){	int c[8]={0};	int i=7,tmp,j;	while(s!=0){		tmp = s%2;		c[i] = tmp;		s = s/2;		i--;	}	for(j=0;j<8;j++){		printf("%d",c[j]);	}}int main(){	int l,n=0,i,j;	char c[30];	int s[3];	while(scanf("%s",c)!=EOF){		l = strlen(c);		for(i=0;i<l;i++){			if(c[i]>='0'&&c[i]<='9'){				j = c[i] - '0';				n = n*10+j;			}else{				bin(n);				printf(".");				n=0;			}			if(i==l-1){				bin(n);			}			}			}	return 0;}