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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
int utf = 0xF42;
string value = Char.ConvertFromUtf32(utf);
int val = Char.ConvertToUtf32(value, 0);
Console.WriteLine( "value is 0x{0:X}" , val);
}
catch (ArgumentOutOfRangeException e) {
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Example 2: For ArgumentOutOfRangeException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
int utf = 0x42F;
string value = Char.ConvertFromUtf32(utf);
Console.WriteLine( "index is not a position within s." );
int val = Char.ConvertToUtf32(value, 0xDFFF);
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
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
string value = null ;
Console.WriteLine( "string value is null" );
int val = Char.ConvertToUtf32(value, 0);
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:
Last Updated :
05 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...