Open In App

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

Last Updated : 05 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: csharp
// 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 initializing 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);
    }
}
}
Output:
value is 0xF42
Example 2: For ArgumentOutOfRangeException csharp
// 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 initializing 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);
    }
}
}
Output:
index is not a position within s.
Exception Thrown: System.ArgumentOutOfRangeException
Example 3: For ArgumentNullException csharp
// 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 initializing 
            // 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);
        }
    }
}
Output:
string value is null
Exception Thrown: System.ArgumentNullException
Reference:

Next Article

Similar Reads