dev/cpp/sockets/int2ip.c

34 lines
557 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>
static const char * int2ip(unsigned int ip) {
static char s[16];
sprintf(s,"%d.%d.%d.%d",ip>>24,ip<<8>>24,ip<<16>>24,ip<<24>>24);
return s;
}
int main(int argc, char * argv[]) {
if(argc < 2) {
perror("need hex ip address");
exit(1);
}
unsigned int ip = 0;
sscanf(argv[1],"%X",&ip);
printf("%s\n",int2ip(ip));
return 0;
}