解析IPv6的使用C [英] IPv6 parsing in C

查看:475
本文介绍了解析IPv6的使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我可以解析的'C'的IPv6地址,并将其转换为一个128位的值?
所以一个十六进制地址像1:22:333:AAAA:B:C:D:E:F需要被转换到它的128位的等效二进制。问题是IP地址可以是类型:: 2及其变体的,因为它们是有效的IPv6地址。

I wanted to know how i can parse an IPv6 address in 'C' and convert it to a 128 bit value? So a hex address like 1:22:333:aaaa:b:c:d:e:f needs to be converted to its 128 bit equivalent binary. The problem is the IP address could be of the type ::2 and its variant since they are valid IPv6 address.

的输入是从键盘,因此是ASCII格式。

The input is from the keyboard and hence is in ASCII format.

任何建议或指针将AP preciated。
谢谢!

Any suggestions or pointers will be appreciated. Thanks!!!

推荐答案

您可以使用POSIX <一个href=\"http://www.opengroup.org/onlinepubs/000095399/functions/inet_ntop.html\"><$c$c>inet_pton将字符串转换为结构in6_addr

You can use POSIX inet_pton to convert a string to a struct in6_addr.

#include <arpa/inet.h>

  ...

const char *ip6str = "::2";
struct in6_addr result;

if (inet_pton(AF_INET6, ip6str, &result) == 1) // success!
{
    //successfully parsed string into "result"
}
else
{
    //failed, perhaps not a valid representation of IPv6?
}

这篇关于解析IPv6的使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆