C# | Char.ConvertToUtf32(String, Int32) Method

This method is used to converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.

Syntax:

public static int ConvertToUtf32 (string s, int index);

Parameters:

s: A string that contains a character or surrogate pair.
index: The index position of the character or surrogate pair in s.

Return Value: This method returns the 21-bit Unicode code point represented by the character or surrogate pair at the position in the s parameter specified by the index parameter.

Exceptions:

  • ArgumentNullException: If s is null.
  • ArgumentOutOfRangeException: if index is not a position within s.
  • Argument Exception: If the index is not a position within s.
  • ArgumentException: If the specified index position contains a surrogate pair, and either the first character in the pair is not a valid high surrogate or the second character in the pair is not a valid low surrogate.

Below programs illustrate the use of Char.ConvertToUtf32(String, Int32) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Char.ConvertToUtf32(String, 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 = 0xF42;
  
        // getting the value
        // using ConvertFromUtf32()
        string value = Char.ConvertFromUtf32(utf);
  
        // getting unicode point
        // using ConvertToUtf32() method
        int val = Char.ConvertToUtf32(value, 0);
  
        // Display the value
        Console.WriteLine("value is 0x{0:X}", val);
    }
      
    catch (ArgumentOutOfRangeException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
}

chevron_right


Output:

value is 0xF42

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Char.ConvertToUtf32(String, 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 = 0x42F;
  
        // getting the value
        // using ConvertFromUtf32()
        string value = Char.ConvertFromUtf32(utf);
  
        // getting unicode point
        // using ConvertToUtf32() method
        Console.WriteLine("index is not a position within s.");
        int val = Char.ConvertToUtf32(value, 0xDFFF);
  
        // Display the value
        Console.WriteLine("value is 0x{0:X}", val);
    }
      
    catch (ArgumentOutOfRangeException e) {
  
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
}

chevron_right


Output:

index is not a position within s.
Exception Thrown: System.ArgumentOutOfRangeException

Example 3: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Char.ConvertToUtf32(String, 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 = 0x42F;
  
            // getting the value
            // using ConvertFromUtf32()
            string value = null;
  
            // getting unicode point
            // using ConvertToUtf32() method
            Console.WriteLine("string value is null");
            int val = Char.ConvertToUtf32(value, 0);
  
            // Display the value
            Console.WriteLine("value is 0x{0:X}", val);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

string value is null
Exception Thrown: System.ArgumentNullException

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.