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.ConvertFromUtf32(Int32) Method in C#
The Char.ConvertFromUtf32(Int32) method in C# converts a specified Unicode code point into a UTF-16 encoded string. This method is particularly useful when working with Unicode characters that may be represented as numeric code points and need to be converted to their corresponding string representation.
Syntax
Following is the syntax −
public static string ConvertFromUtf32(int utf32);
Parameters
utf32: A 21-bit Unicode code point representing a valid Unicode character. The valid range is from 0x000000 to 0x10FFFF, excluding the surrogate pair range (0xD800 to 0xDFFF).
Return Value
Returns a string object consisting of one or two 16-bit code units (UTF-16) that represent the Unicode character specified by the utf32 parameter.
Examples
Basic Unicode Character Conversion
This example demonstrates converting basic Unicode code points to their character representations −
using System;
public class Demo {
public static void Main() {
int utf1 = 0x0051; // 'Q'
int utf2 = 0x0046; // 'F'
int utf3 = 0x0041; // 'A'
string str1 = Char.ConvertFromUtf32(utf1);
string str2 = Char.ConvertFromUtf32(utf2);
string str3 = Char.ConvertFromUtf32(utf3);
Console.WriteLine("Code point 0x0051 = " + str1);
Console.WriteLine("Code point 0x0046 = " + str2);
Console.WriteLine("Code point 0x0041 = " + str3);
}
}
The output of the above code is −
Code point 0x0051 = Q Code point 0x0046 = F Code point 0x0041 = A
Working with Special Characters and Symbols
This example shows how to convert Unicode code points for special characters and symbols −
using System;
public class Demo {
public static void Main() {
int heart = 0x2665; // Heart symbol
int smiley = 0x263A; // Smiley face
int copyright = 0x00A9; // Copyright symbol
int euro = 0x20AC; // Euro symbol
Console.WriteLine("Heart: " + Char.ConvertFromUtf32(heart));
Console.WriteLine("Smiley: " + Char.ConvertFromUtf32(smiley));
Console.WriteLine("Copyright: " + Char.ConvertFromUtf32(copyright));
Console.WriteLine("Euro: " + Char.ConvertFromUtf32(euro));
}
}
The output of the above code is −
Heart: ? Smiley: ? Copyright: © Euro: ?
Converting Decimal Code Points
This example demonstrates converting decimal Unicode values to characters −
using System;
public class Demo {
public static void Main() {
int[] codePoints = { 65, 66, 67, 8364, 169 }; // A, B, C, ?, ©
Console.WriteLine("Converting decimal code points:");
foreach (int code in codePoints) {
string character = Char.ConvertFromUtf32(code);
Console.WriteLine($"Code {code} = '{character}'");
}
}
}
The output of the above code is −
Converting decimal code points: Code 65 = 'A' Code 66 = 'B' Code 67 = 'C' Code 8364 = '?' Code 169 = '©'
Key Points
The method accepts Unicode code points in the range 0x000000 to 0x10FFFF.
Code points in the surrogate pair range (0xD800 to 0xDFFF) will throw an
ArgumentOutOfRangeException.The returned string may contain one or two UTF-16 code units depending on the Unicode character.
This method is useful for converting numeric Unicode representations to displayable characters.
Conclusion
The Char.ConvertFromUtf32(Int32) method provides a straightforward way to convert Unicode code points to their string representations. It's essential for applications that work with Unicode data and need to convert numeric code points to displayable characters.
