IdnMapping.GetUnicode 方法

定义

将根据 IDNA 标准编码的一个或多个域名标签解码为 Unicode 字符字符串。

重载

名称 说明
GetUnicode(String)

将一个或多个域名标签的字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

GetUnicode(String, Int32)

将一个或多个域名标签的子字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

GetUnicode(String, Int32, Int32)

将指定长度的子字符串解码为 Unicode 字符字符串,其中包含一个或多个域名标签(根据 IDNA 标准编码)。

GetUnicode(String)

Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs

将一个或多个域名标签的字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

public:
 System::String ^ GetUnicode(System::String ^ ascii);
public string GetUnicode(string ascii);
member this.GetUnicode : string -> string
Public Function GetUnicode (ascii As String) As String

参数

ascii
String

要解码的字符串,该字符串由根据 IDNA 标准编码的 US-ASCII 字符范围(U+0020 到 U+007E)中的一个或多个标签组成。

返回

参数指定的 ascii IDNA 子字符串的 Unicode 等效项。

例外

asciinull

ascii 基于 AllowUnassigned 属性和 UseStd3AsciiRules IDNA 标准无效。

示例

以下示例使用 GetAscii(String) 此方法将国际化域名数组转换为 Punycode。 然后,该方法 GetUnicode(String) 将 Punycode 域名转换回原始域名,但将原始标签分隔符替换为标准标签分隔符。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] names = { "bücher.com", "мойдомен.рф", "παράδειγμα.δοκιμή",
                         "mycharity\u3002org",
                         "prose\u0000ware.com", "proseware..com", "a.org",
                         "my_company.com" };
      IdnMapping idn = new IdnMapping();

      foreach (var name in names) {
         try {
            string punyCode = idn.GetAscii(name);
            string name2 = idn.GetUnicode(punyCode);
            Console.WriteLine("{0} --> {1} --> {2}", name, punyCode, name2);
            Console.WriteLine("Original: {0}", ShowCodePoints(name));
            Console.WriteLine("Restored: {0}", ShowCodePoints(name2));
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a valid domain name.", name);
         }
         Console.WriteLine();
      }
   }

   private static string ShowCodePoints(string str1)
   {
      string output = "";
      foreach (var ch in str1)
         output += $"U+{(ushort)ch:X4} ";

      return output;
   }
}
// The example displays the following output:
//    bücher.com --> xn--bcher-kva.com --> bücher.com
//    Original: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
//    Restored: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
//
//    мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф
//    Original: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
//    Restored: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
//
//    παράδειγμα.δοκιμή --> xn--hxajbheg2az3al.xn--jxalpdlp --> παράδειγμα.δοκιμή
//    Original: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
//    Restored: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
//
//    mycharity。org --> mycharity.org --> mycharity.org
//    Original: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+3002 U+006F U+0072 U+0067
//    Restored: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+002E U+006F U+0072 U+0067
//
//    prose ware.com is not a valid domain name.
//
//    proseware..com is not a valid domain name.
//
//    a.org --> a.org --> a.org
//    Original: U+0061 U+002E U+006F U+0072 U+0067
//    Restored: U+0061 U+002E U+006F U+0072 U+0067
//
//    my_company.com --> my_company.com --> my_company.com
//    Original: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
//    Restored: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim names() As String = { "bücher.com", "мойдомен.рф", "παράδειγμα.δοκιμή",
                                "mycharity" + ChrW(&h3002) + "org",
                                "prose" + ChrW(0) + "ware.com", "proseware..com", "a.org", 
                                "my_company.com" }
      Dim idn As New IdnMapping()
      
      For Each name In names
         Try
            Dim punyCode As String = idn.GetAscii(name)
            Dim name2 As String = idn.GetUnicode(punyCode)
            Console.WriteLine("{0} --> {1} --> {2}", name, punyCode, name2) 
            Console.WriteLine("Original: {0}", ShowCodePoints(name))
            Console.WriteLine("Restored: {0}", ShowCodePoints(name2))
         Catch e As ArgumentException 
            Console.WriteLine("{0} is not a valid domain name.", name)
         End Try
         Console.WriteLine()
      Next   
   End Sub
   
   Private Function ShowCodePoints(str1 As String) As String
      Dim output As String = ""
      For Each ch In str1
         output += String.Format("U+{0} ", Convert.ToUInt16(ch).ToString("X4"))
      Next
      Return output
   End Function
End Module
' The example displays the following output:
'    bücher.com --> xn--bcher-kva.com --> bücher.com
'    Original: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
'    Restored: U+0062 U+00FC U+0063 U+0068 U+0065 U+0072 U+002E U+0063 U+006F U+006D
'    
'    мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф
'    Original: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
'    Restored: U+043C U+043E U+0439 U+0434 U+043E U+043C U+0435 U+043D U+002E U+0440 U+0444
'    
'    παράδειγμα.δοκιμή --> xn--hxajbheg2az3al.xn--jxalpdlp --> παράδειγμα.δοκιμή
'    Original: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
'    Restored: U+03C0 U+03B1 U+03C1 U+03AC U+03B4 U+03B5 U+03B9 U+03B3 U+03BC U+03B1 U+002E U+03B4 U+03BF U+03BA U+03B9 U+03BC U+03AE
'    
'    mycharity。org --> mycharity.org --> mycharity.org
'    Original: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+3002 U+006F U+0072 U+0067
'    Restored: U+006D U+0079 U+0063 U+0068 U+0061 U+0072 U+0069 U+0074 U+0079 U+002E U+006F U+0072 U+0067
'    
'    prose ware.com is not a valid domain name.
'    
'    proseware..com is not a valid domain name.
'    
'    a.org --> a.org --> a.org
'    Original: U+0061 U+002E U+006F U+0072 U+0067
'    Restored: U+0061 U+002E U+006F U+0072 U+0067
'    
'    my_company.com --> my_company.com --> my_company.com
'    Original: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D
'    Restored: U+006D U+0079 U+005F U+0063 U+006F U+006D U+0070 U+0061 U+006E U+0079 U+002E U+0063 U+006F U+006D

注解

有关域名、标签和标签分隔符的详细信息,请参阅该方法 IdnMapping.GetAscii(String, Int32, Int32) 的备注。

适用于

GetUnicode(String, Int32)

Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs

将一个或多个域名标签的子字符串(根据 IDNA 标准编码)解码为 Unicode 字符字符串。

public:
 System::String ^ GetUnicode(System::String ^ ascii, int index);
public string GetUnicode(string ascii, int index);
member this.GetUnicode : string * int -> string
Public Function GetUnicode (ascii As String, index As Integer) As String

参数

ascii
String

要解码的字符串,该字符串由根据 IDNA 标准编码的 US-ASCII 字符范围(U+0020 到 U+007E)中的一个或多个标签组成。

index
Int32

从零开始的偏移 ascii 量,指定要解码的子字符串的开始。 解码操作继续到字符串的 ascii 末尾。

返回

与参数指定的 asciiindex IDNA 子字符串的 Unicode 等效项。

例外

asciinull

index 小于零。

-或-

index 大于长度 ascii

ascii 基于 AllowUnassigned 属性和 UseStd3AsciiRules IDNA 标准无效。

注解

有关域名、标签和标签分隔符的详细信息,请参阅该方法 IdnMapping.GetAscii(String, Int32, Int32) 的备注。

适用于

GetUnicode(String, Int32, Int32)

Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs
Source:
IdnMapping.cs

将指定长度的子字符串解码为 Unicode 字符字符串,其中包含一个或多个域名标签(根据 IDNA 标准编码)。

public:
 System::String ^ GetUnicode(System::String ^ ascii, int index, int count);
public string GetUnicode(string ascii, int index, int count);
member this.GetUnicode : string * int * int -> string
Public Function GetUnicode (ascii As String, index As Integer, count As Integer) As String

参数

ascii
String

要解码的字符串,该字符串由根据 IDNA 标准编码的 US-ASCII 字符范围(U+0020 到 U+007E)中的一个或多个标签组成。

index
Int32

从零开始的偏移量 ascii ,指定子字符串的开始位置。

count
Int32

在字符串中指定的 index 位置开始的子字符串中要转换的 ascii 字符数。

返回

由参数和参数指定的 asciiindexcount IDNA 子字符串的 Unicode 等效项。

例外

asciinull

indexcount 小于零。

-或-

index 大于长度 ascii

-或-

index大于减ascii号的count长度。

ascii 基于 AllowUnassigned 属性和 UseStd3AsciiRules IDNA 标准无效。

注解

有关域名、标签和标签分隔符的详细信息,请参阅该方法 IdnMapping.GetAscii(String, Int32, Int32) 的备注。

适用于