📜  C#| Char.ConvertFromUtf32(Int32)方法

📅  最后修改于: 2021-05-29 16:44:39             🧑  作者: Mango

此方法用于将指定的Unicode代码点转换为UTF-16编码的字符串。

句法:

public static string ConvertFromUtf32 (int utf32);

此处, utf32是21位Unicode代码点。

返回值:该方法返回一个字符串,该字符串由一个字符对象或一对替代Char对象组成,该字符串与utf32参数指定的代码点等效。

异常:如果utf32不是范围从U + 0U + 10FFFF的有效21位Unicode代码点,则该方法将返回ArgumentOutOfRangeException ,不包括从U + D800U + 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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.char.convertfromutf32?view=netframework-4.7.2