如何将IPv6地址的缩短/替代格式转换为首选的IPv6地址 [英] How to convert IPv6 address shortened/alternative formats to preferred IPv6 address

查看:595
本文介绍了如何将IPv6地址的缩短/替代格式转换为首选的IPv6地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将缩短的IPv6地址/替代格式转换为首选的IPv6地址? .net中可以使用任何方法吗?

How to convert IPv6 address shortened/alternative formats to preferred IPv6 address ? Is there any method in .net can be used?

优选的IPv6地址表示为:< tt class ="xph"> xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx</tt>其中每个< tt class ="xph"> x</tt是代表4位的十六进制数字.

The preferred IPv6 address representation is: <tt class="xph">xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx</tt> where each <tt class="xph">x</tt> is a hexadecimal digit representing 4 bits.

除了这种首选格式外,IPv6地址还可以用其他两种缩短的格式指定:

In addition to this preferred format, IPv6 addresses may be specified in two other shortened formats:

  • 忽略前导零
    通过省略前导零来指定IPv6地址.例如,IPv6地址< tt class ="xph"> 1050:0000:0000:0000:0005:0600:300c:326b</tt可以写为< tt class ="xph"> 1050:0:0:0:5:600:300c:326b</tt>.
  • 双冒号
    通过使用双冒号(< tt class ="xph" ::</tt>)"代替一系列零来指定IPv6地址.例如,IPv6地址< tt class ="xph"> ff06:0:0:0:0:0:0:0:c3</tt>可以被写为< tt class ="xph"> ff06 :: c3</tt>.双冒号只能在 IP地址.
  • Omit leading zeros
    Specify IPv6 addresses by omitting leading zeros. For example, IPv6 address <tt class="xph">1050:0000:0000:0000:0005:0600:300c:326b</tt> may be written as <tt class="xph">1050:0:0:0:5:600:300c:326b</tt>.
  • Double colon
    Specify IPv6 addresses by using double colons (<tt class="xph">::</tt>) in place of a series of zeros. For example, IPv6 address <tt class="xph">ff06:0:0:0:0:0:0:c3</tt> may be written as <tt class="xph">ff06::c3</tt>. Double colons may be used only once in an IP address.

这两种IPv6地址使用这种替代格式:

These two types of IPv6 addresses use this alternative format:

  • IPv4映射的IPv6地址
    此类型的地址用于将IPv4节点表示为IPv6地址.它允许IPv6应用程序直接与IPv4应用程序进行通信.例如,< tt class ="xph"> 0:0:0:0:0:ffff:192.1.56.10</tt>和< tt class ="xph"> :: ffff:192.1.56.10/96</tt (缩短 格式).
  • 与IPv4兼容的IPv6地址
    这种类型的地址用于隧道.它允许IPv6节点跨IPv4基础结构进行通信.例如,< tt class ="xph"> 0:0:0:0:0:0:192.1.56.10</tt>和< tt class ="xph"> :: 192.1.56.10/96< tt (缩短格式).
  • IPv4–mapped IPv6 address
    This type of address is used to represent IPv4 nodes as IPv6 addresses. It allows IPv6 applications to communicate directly with IPv4 applications. For example, <tt class="xph">0:0:0:0:0:ffff:192.1.56.10</tt> and <tt class="xph">::ffff:192.1.56.10/96</tt> (shortened format).
  • IPv4–compatible IPv6 address
    This type of address is used for tunneling. It allows IPv6 nodes to communicate across an IPv4 infrastructure. For example, <tt class="xph">0:0:0:0:0:0:192.1.56.10</tt> and <tt class="xph">::192.1.56.10/96</tt> (shortened format).

Elaine

推荐答案

我能想到的最简单的解决方案是使用IPAddress.TryParse从字符串获取地址,然后使用IPAddress.GetAddressBytes获取单个字节,最后使用string.Format将其重新格式化为首选格式.乏味的,但是 应该工作:

The simplest solution I can think of is to obtain the address from the string using IPAddress.TryParse, then use IPAddress.GetAddressBytes to get the individual bytes and finally use string.Format to reformat them into the preferred format. Tedious, but should work:

      var addressToParse = "::ffff:192.168.1.99";

      IPAddress address;
      if (IPAddress.TryParse (addressToParse, out address)) {
        if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
          var bytes = address.GetAddressBytes ();
          var preferredFormatString = string.Format ("{0:x2}{1:x2}:{2:x2}{3:x2}:{4:x2}{5:x2}:{6:x2}{7:x2}:{8:x2}{9:x2}:{10:x2}{11:x2}:{12:x2}{13:x2}:{14:x2}{15:x2}", bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5], bytes [6], bytes [7], bytes [8], bytes [9], bytes [10], bytes [11], bytes [12], bytes [13], bytes [14], bytes [15]);
          Console.WriteLine (preferredFormatString);
        }
      }

HTH
--mc

HTH
--mc


这篇关于如何将IPv6地址的缩短/替代格式转换为首选的IPv6地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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