如何将string转in6_addr

shn521 2012-10-25 04:45:31
如题如何将string转in6_addr,感谢啊!
...全文
715 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Huer 2013-10-26
  • 打赏
  • 举报
回复
turingo 回复的是linux下编码;我想知道windows下怎样将ipv6地址转换为in6_addr,谢谢啊~~
十八道胡同 2012-10-25
  • 打赏
  • 举报
回复
const char* ip
struct sockaddr_in svraddr;
svraddr.sin_family = AF_INET;
svraddr.sin_addr.s_addr = inet_addr(ip);
svraddr.sin_port = htons(port);
十八道胡同 2012-10-25
  • 打赏
  • 举报
回复
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;
}
图灵狗 2012-10-25
  • 打赏
  • 举报
回复 1

#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;
}

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧