首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么将string转in6_addr

2012-11-09 
如何将string转in6_addr如题如何将string转in6_addr,感谢啊![解决办法]C/C++ code#include stdio.h#incl

如何将string转in6_addr
如题如何将string转in6_addr,感谢啊!

[解决办法]

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>int main(void){    char IPdotdec[20] = "192.168.0.1";    struct in6_addr s;    inet_pton(AF_INET6, IPdotdec, (void *)&s);    inet_ntop(AF_INET6, (void *)&s, IPdotdec, 16);    printf("inet_ntop: %s\n", IPdotdec);    return 0;}
[解决办法]
char* hexToCharIP(struct in_addr addrIP)
{
char* ip;
unsigned int intIP;
memcpy(&intIP, &addrIP,sizeof(unsigned int));
int a = (intIP >> 24) & 0xFF;
int b = (intIP >> 16) & 0xFF;
int c = (intIP >> 8) & 0xFF;
int d = intIP & 0xFF;
if((ip = (char*)malloc(16*sizeof(char))) == NULL)
{
return NULL;
}
sprintf(ip, "%d.%d.%d.%d", d,c,b,a);
return ip;
}

热点排行