dev/c/echo_server-udp/int2ip.c

36 lines
578 B
C
Raw Normal View History

2011-05-04 15:30:24 +04:00
#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;
}