Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Char.ConvertToUtf32(String, Int32) Method in C#
The Char.ConvertToUtf32(String, Int32) method in C# is used to convert the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point. This method is particularly useful when working with Unicode characters that may be represented as surrogate pairs in UTF-16 encoding.
Syntax
Following is the syntax −
public static int ConvertToUtf32(string s, int index);
Parameters
-
s − The string that contains a character or surrogate pair.
-
index − The index position of the character or surrogate pair in the string.
Return Value
Returns the 21-bit Unicode code point represented by the character or surrogate pair at the specified position in the string.
Using ConvertToUtf32 with Basic Characters
Example
using System;
public class Demo {
public static void Main() {
int utf = 0x046;
string str = Char.ConvertFromUtf32(utf);
Console.WriteLine("Character = " + str);
int res = Char.ConvertToUtf32(str, 0);
Console.WriteLine("Unicode Code Point = 0x{0:X}", res);
}
}
The output of the above code is −
Character = F Unicode Code Point = 0x46
Using ConvertToUtf32 with Different Characters
Example
using System;
public class Demo {
public static void Main() {
string text = "Hello World!";
for (int i = 0; i < text.Length; i++) {
int codePoint = Char.ConvertToUtf32(text, i);
Console.WriteLine("Character '{0}' at index {1} = U+{2:X4}",
text[i], i, codePoint);
}
}
}
The output of the above code is −
Character 'H' at index 0 = U+0048 Character 'e' at index 1 = U+0065 Character 'l' at index 2 = U+006C Character 'l' at index 3 = U+006C Character 'o' at index 4 = U+006F Character ' ' at index 5 = U+0020 Character 'W' at index 6 = U+0057 Character 'o' at index 7 = U+006F Character 'r' at index 8 = U+0072 Character 'l' at index 9 = U+006C Character 'd' at index 10 = U+0064 Character '!' at index 11 = U+0021
Using ConvertToUtf32 with Surrogate Pairs
Example
using System;
public class Demo {
public static void Main() {
// High surrogate pair example (Emoji)
int emojiCodePoint = 0x1F600; // Grinning face emoji
string emoji = Char.ConvertFromUtf32(emojiCodePoint);
Console.WriteLine("Emoji: " + emoji);
Console.WriteLine("String length: " + emoji.Length);
// Convert back to code point
int result = Char.ConvertToUtf32(emoji, 0);
Console.WriteLine("Code point: U+{0:X}", result);
Console.WriteLine("Original matches result: " + (emojiCodePoint == result));
}
}
The output of the above code is −
Emoji: ? String length: 2 Code point: U+1F600 Original matches result: True
Conclusion
The Char.ConvertToUtf32(String, Int32) method converts UTF-16 encoded characters or surrogate pairs at a specified string index into Unicode code points. This method is essential when working with Unicode text that may contain characters beyond the Basic Multilingual Plane, ensuring proper character handling in internationalized applications.
