日期:2014-05-16 浏览次数:20601 次
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <netdb.h> char *ip_h2s(const char *h, char *s) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_addr.s_addr = htonl(strtoul(h, NULL, 16)); strcpy(s, inet_ntoa(addr.sin_addr)); return s; } char *ip_h2s_simple(const char *h, char *s) { unsigned int n[4]; sscanf(h, "%02x%02x%02x%02x", &n[0], &n[1], &n[2], &n[3]); sprintf(s, "%d.%d.%d.%d", n[0], n[1], n[2], n[3]); return s; } int main(int argc, char *argv[]) { char s[16]; printf("%s\n", ip_h2s("C0A80101", s)); printf("%s\n", ip_h2s_simple("C0A80101", s)); return 0; }