Char.ConvertToUtf32 Method

Definition

Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.

Overloads

ConvertToUtf32(Char, Char)

Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.

ConvertToUtf32(String, Int32)

Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.

Examples

The following code example demonstrates the ConvertToUtf32 and ConvertFromUtf32 methods.

// This example demonstrates the Char.ConvertFromUtf32() method
//                           and Char.ConvertToUtf32() overloads.
using System;

class Sample
{
    public static void Main()
    {
    int letterA = 0x0041;  //U+00041 = LATIN CAPITAL LETTER A
    int music   = 0x1D161; //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
    string s1;
    string comment   = "Create a UTF-16 encoded string from a code point.";
    string comment1b = "Create a code point from a UTF-16 encoded string.";
    string comment2b = "Create a code point from a surrogate pair at a certain position in a string.";
    string comment2c = "Create a code point from a high surrogate and a low surrogate code point.";

//  Convert code point U+0041 to UTF-16. The UTF-16 equivalent of
//  U+0041 is a Char with hexadecimal value 0041.

    Console.WriteLine(comment);
    s1 = Char.ConvertFromUtf32(letterA);
    Console.Write("    1a) 0x{0:X} => ", letterA);
    Show(s1);
    Console.WriteLine();

//  Convert the lone UTF-16 character to a code point.

    Console.WriteLine(comment1b);
    letterA = Char.ConvertToUtf32(s1, 0);
    Console.Write("    1b) ");
    Show(s1);
    Console.WriteLine(" => 0x{0:X}", letterA);
    Console.WriteLine();

// -------------------------------------------------------------------

//  Convert the code point U+1D161 to UTF-16. The UTF-16 equivalent of
//  U+1D161 is a surrogate pair with hexadecimal values D834 and DD61.

    Console.WriteLine(comment);
    s1 = Char.ConvertFromUtf32(music);
    Console.Write("    2a) 0x{0:X} => ", music);
    Show(s1);
    Console.WriteLine();

//  Convert the surrogate pair in the string at index position
//  zero to a code point.

    Console.WriteLine(comment2b);
    music = Char.ConvertToUtf32(s1, 0);
    Console.Write("    2b) ");
    Show(s1);
    Console.WriteLine(" => 0x{0:X}", music);

//  Convert the high and low characters in the surrogate pair into a code point.

    Console.WriteLine(comment2c);
    music = Char.ConvertToUtf32(s1[0], s1[1]);
    Console.Write("    2c) ");
    Show(s1);
    Console.WriteLine(" => 0x{0:X}", music);
    }

    private static void Show(string s)
    {
    for (int x = 0; x < s.Length; x++)
        {
        Console.Write("0x{0:X}{1}",
                       (int)s[x],
                       ((x == s.Length-1)? String.Empty : ", "));
        }
    }
}
/*
This example produces the following results:

Create a UTF-16 encoded string from a code point.
    1a) 0x41 => 0x41
Create a code point from a UTF-16 encoded string.
    1b) 0x41 => 0x41

Create a UTF-16 encoded string from a code point.
    2a) 0x1D161 => 0xD834, 0xDD61
Create a code point from a surrogate pair at a certain position in a string.
    2b) 0xD834, 0xDD61 => 0x1D161
Create a code point from a high surrogate and a low surrogate code point.
    2c) 0xD834, 0xDD61 => 0x1D161

*/

ConvertToUtf32(Char, Char)

Source:
Char.cs
Source:
Char.cs
Source:
Char.cs

Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.

public static int ConvertToUtf32 (char highSurrogate, char lowSurrogate);

Parameters

highSurrogate
Char

A high surrogate code unit (that is, a code unit ranging from U+D800 through U+DBFF).

lowSurrogate
Char

A low surrogate code unit (that is, a code unit ranging from U+DC00 through U+DFFF).

Returns

The 21-bit Unicode code point represented by the highSurrogate and lowSurrogate parameters.

Exceptions

highSurrogate is not in the range U+D800 through U+DBFF, or lowSurrogate is not in the range U+DC00 through U+DFFF.

Remarks

Use this method to convert a surrogate pair into a 21-bit Unicode code point. To convert UTF-16 data into UTF-32 data, use the System.Text.UTF32Encoding class.

Ordinarily, UTF-16 encoding represents a single Unicode character as a 16-bit code unit. However, it also supports surrogate pairs, which allow a single abstract character to be represented as two 16-bit code units. These two Char objects must have code units that range from U+D800 to U+DBFF for the first (high) surrogate and from U+DC00 to U+DFFF for the second (low) surrogate. Surrogate pairs are supported only by UTF-16 encoding. This method allows a character represented by a UTF-16 surrogate pair to be converted to a character using UTF-32 encoding.

See also

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ConvertToUtf32(String, Int32)

Source:
Char.cs
Source:
Char.cs
Source:
Char.cs

Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.

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

Parameters

s
String

A string that contains a character or surrogate pair.

index
Int32

The index position of the character or surrogate pair in s.

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

index is not a position within s.

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.

Remarks

Use this method to convert a character or surrogate pair into a 21-bit Unicode code point. To convert UTF-16 data into UTF-32 data, use the System.Text.UTF32Encoding class.

See also

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0