dev/c/echo_server/ip2int.c

31 lines
510 B
C

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
unsigned int ip2int(const char * s) {
int ip[4];
sscanf(s,"%d.%d.%d.%d",ip,ip+1,ip+2,ip+3);
return ip[0]<<24|ip[1]<<16|ip[2]<<8|ip[3];
}
int main(int argc, char * argv[]) {
if(argc < 2) {
perror("need ip address string");
exit(1);
}
printf("%X\n",ip2int(argv[1]));
return 0;
}