IdnMapping.GetUnicode 方法

定義

將一個或多個根據 IDNA 標準編碼的網域名稱標籤解碼為 Unicode 字元的字串。

多載

GetUnicode(String)

將根據 IDNA 標準編碼的一個或多個網域名稱標籤字串解碼為 Unicode 字元字串。

GetUnicode(String, Int32)

將根據 IDNA 標準編碼的一個或多個網域名稱標籤子字串解碼為 Unicode 字元字串。

GetUnicode(String, Int32, Int32)

將根據 IDNA 標準編碼的一個或多個網域名稱標籤的指定長度子字串,解碼為 Unicode 字元字串。

GetUnicode(String)

來源:
IdnMapping.cs
來源:
IdnMapping.cs
來源:
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

要解碼的字串,其包含 US-ASCII 字元範圍 (U+0020 到 U+007E) 中一個或多個標籤,根據 IDNA 標準編碼。

傳回

IDNA 子字串的 Unicode 對等用法,由 ascii 參數指定。

例外狀況

asciinull

根據 AllowUnassignedUseStd3AsciiRules 屬性以及 IDNA 標準,ascii 無效。

範例

下列範例會 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)

來源:
IdnMapping.cs
來源:
IdnMapping.cs
來源:
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

要解碼的字串,其包含 US-ASCII 字元範圍 (U+0020 到 U+007E) 中一個或多個標籤,根據 IDNA 標準編碼。

index
Int32

針對 ascii 以零為起始的位移,指定要解碼的子字串起點。 解碼作業會持續到 ascii 字串尾端。

傳回

asciiindex 參數指定 IDNA 子字串的 Unicode 對等用法。

例外狀況

asciinull

index 小於零。

-或-

index 大於 ascii 的長度。

根據 AllowUnassignedUseStd3AsciiRules 屬性以及 IDNA 標準,ascii 無效。

備註

如需功能變數名稱、標籤和標籤分隔符的詳細資訊,請參閱 方法的 IdnMapping.GetAscii(String, Int32, Int32) 備註。

適用於

GetUnicode(String, Int32, Int32)

來源:
IdnMapping.cs
來源:
IdnMapping.cs
來源:
IdnMapping.cs

將根據 IDNA 標準編碼的一個或多個網域名稱標籤的指定長度子字串,解碼為 Unicode 字元字串。

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

要解碼的字串,其包含 US-ASCII 字元範圍 (U+0020 到 U+007E) 中一個或多個標籤,根據 IDNA 標準編碼。

index
Int32

針對 ascii 以零起始的位移,指定子字串起點。

count
Int32

子字串中要轉換的字元數,此子字串從 ascii 字串中 index 所指定的位置開始。

傳回

asciiindexcount 參數指定 IDNA 子字串的 Unicode 對等用法。

例外狀況

asciinull

indexcount 小於零。

-或-

index 大於 ascii 的長度。

-或-

index 大於 ascii 減去 count 之後的長度。

根據 AllowUnassignedUseStd3AsciiRules 屬性以及 IDNA 標準,ascii 無效。

備註

如需功能變數名稱、標籤和標籤分隔符的詳細資訊,請參閱 方法的 IdnMapping.GetAscii(String, Int32, Int32) 備註。

適用於