當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Char.ConvertFromUtf32(Int32)用法及代碼示例


此方法用於將指定的Unicode代碼點轉換為UTF-16編碼的字符串。

用法:

public static string ConvertFromUtf32 (int utf32);

此處,utf32是21位Unicode代碼點。


返回值:此方法返回一個字符串,該字符串由一個Char對象或一組替代Char對象組成,這些字符串與utf32參數指定的代碼點等效。

異常:此方法返回ArgumentOutOfRangeException如果utf32不是從U + 0到U + 10FFFF的有效21位Unicode代碼點,則不包括從U + D800到U + DFFF的代理對範圍。

以下示例程序旨在說明Char.ConvertFromUtf32(Int32)方法的用法:

範例1:

// C# program to demonstrate 
// Char.ConvertFromUtf32(Int32) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // declaring and intializing  
            // int variablewith 21 bit  
            // unicode 
            int utf = 0x0042; 
  
            // getting the value 
            // using ConvertFromUtf32() 
            string value = Char.ConvertFromUtf32(utf); 
  
            // Display the value 
            Console.WriteLine("value is {0}", value); 
        } 
        catch (ArgumentOutOfRangeException e) { 
  
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
value is B

範例2:對於ArgumentOutOfRangeException

// C# program to demonstrate 
// Char.ConvertFromUtf32(Int32) 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // declaring and intializing  
            // int variable with 21 bit  
            // unicode 
            int utf = 0x11FFFF; 
  
            // getting the value 
            // using ConvertFromUtf32() 
            Console.WriteLine("0x11FFFF is excedding the limit"); 
            string value = Char.ConvertFromUtf32(utf); 
  
            // Display the value 
            Console.WriteLine("value is {0}", value); 
        } 
        catch (ArgumentOutOfRangeException e) { 
  
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
0x11FFFF is excedding the limit
Exception Thrown:System.ArgumentOutOfRangeException

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Char.ConvertFromUtf32(Int32) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。