当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。