C# | Char.ConvertFromUtf32(Int32) Method

This method is used to converts the specified Unicode code point into a UTF-16 encoded string.

Syntax:

public static string ConvertFromUtf32 (int utf32);

Here, utf32 is a 21-bit Unicode code point.

Return Value: This method returns a string consisting of one Char object or a surrogate pair of Char objects equivalent to the code point specified by the utf32 parameter.

Exception: This method returns the ArgumentOutOfRangeException if utf32 is not a valid 21-bit Unicode code point ranging from U+0 through U+10FFFF, excluding the surrogate pair range from U+D800 through U+DFFF.

Below programs illustrate the use of Char.ConvertFromUtf32(Int32) Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

value is B

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

0x11FFFF is excedding the limit
Exception Thrown: System.ArgumentOutOfRangeException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.