首页 > 代码库 > C语言IP 转换

C语言IP 转换

#include <stdio.h>

void ip2s(int n){
  unsigned char *p = &n;
  printf("%u.%u.%u.%u\n",*p,*(p+1),*(p+2),*(p+3));
}

int s2ip(char s[]){
  int  a=0;
  int  b=0;
  int  c=0;
  int  d=0;
  sscanf(s,"%d.%d.%d.%d",&a,&b,&c,&d);
 
 int ip=0;
 char *p = &ip;
 *p = a;
 p++;
 *p = b;
 p++;
 *p=c;
 p++;
 *p=d;
 return ip;
}

int main(void){
   
   char ip[100]="192.168.1.105";
   int intip = s2ip(ip);
   ip2s(intip);
   return 0;
}

 

C语言IP 转换