Char.ConvertToUtf32 方法
定义
将 UTF-16 编码的代理项对的值转换为 Unicode 码位。Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.
重载
| ConvertToUtf32(Char, Char) |
将 UTF-16 编码的代理项对的值转换为 Unicode 码位。Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point. |
| ConvertToUtf32(String, Int32) |
将字符串中指定位置的 UTF-16 编码字符或代理项对的值转换为 Unicode 码位。Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point. |
示例
下面的代码示例演示了 ConvertToUtf32 和 ConvertFromUtf32 方法。The following code example demonstrates the ConvertToUtf32 and ConvertFromUtf32 methods.
// This example demonstrates the Char.ConvertFromUtf32() method
// and Char.ConvertToUtf32() overloads.
using namespace System;
void Show( String^ s )
{
// Console::Write( "0x{0:X}, 0x{1:X}", (int)s->get_Chars( 0 ), (int)s->get_Chars( 1 ) );
Console::Write( "0x{0:X}, 0x{1:X}", (int)s[ 0 ], (int)s[ 1 ] );
}
int main()
{
int music = 0x1D161; //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
String^ s1;
String^ comment1a = "Create a UTF-16 encoded string from a code point.";
String^ comment1b = "Create a code point from a surrogate pair at a certain position in a string.";
String^ comment1c = "Create a code point from a high surrogate and a low surrogate code point.";
// -------------------------------------------------------------------
// 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( comment1a );
s1 = Char::ConvertFromUtf32( music );
Console::Write( " 1a) 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( comment1b );
music = Char::ConvertToUtf32( s1, 0 );
Console::Write( " 1b) " );
Show( s1 );
Console::WriteLine( " => 0x{0:X}", music );
// Convert the high and low characters in the surrogate pair into a code point.
Console::WriteLine( comment1c );
music = Char::ConvertToUtf32( s1[ 0 ], s1[ 1 ] );
Console::Write( " 1c) " );
Show( s1 );
Console::WriteLine( " => 0x{0:X}", music );
}
/*
This example produces the following results:
Create a UTF-16 encoded string from a code point.
1a) 0x1D161 => 0xD834, 0xDD61
Create a code point from a surrogate pair at a certain position in a string.
1b) 0xD834, 0xDD61 => 0x1D161
Create a code point from a high surrogate and a low surrogate code point.
1c) 0xD834, 0xDD61 => 0x1D161
*/
// 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
*/
Class Sample
Public Shared Sub Main()
Dim letterA As Integer = &H41 'U+00041 = LATIN CAPITAL LETTER A
Dim music As Integer = &H1D161 'U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
Dim s1 As String
Dim comment As String = "Create a UTF-16 encoded string from a code point."
Dim comment1b As String = "Create a code point from a UTF-16 encoded string."
Dim comment2b As String = "Create a code point from a surrogate pair at a certain position in a string."
Dim comment2c As String = "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.Chars(0), s1.Chars(1))
Console.Write(" 2c) ")
Show(s1)
Console.WriteLine(" => 0x{0:X}", music)
End Sub
Private Shared Sub Show(s As String)
Dim x As Integer
If s.Length = 0 Then Exit Sub
For x = 0 To s.Length - 1
Console.Write("0x{0:X}{1}", _
AscW(s.Chars(x)), _
IIf(x = s.Length - 1, [String].Empty, ", "))
Next
End Sub
End Class
'
'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)
将 UTF-16 编码的代理项对的值转换为 Unicode 码位。Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.
public:
static int ConvertToUtf32(char highSurrogate, char lowSurrogate);
public static int ConvertToUtf32 (char highSurrogate, char lowSurrogate);
static member ConvertToUtf32 : char * char -> int
Public Shared Function ConvertToUtf32 (highSurrogate As Char, lowSurrogate As Char) As Integer
参数
- highSurrogate
- Char
高代理项代码单位(即代码单位从 U+D800 到 U+DBFF)。A high surrogate code unit (that is, a code unit ranging from U+D800 through U+DBFF).
- lowSurrogate
- Char
低代理项代码单位(即代码单位从 U+DC00 到 U+DFFF)。A low surrogate code unit (that is, a code unit ranging from U+DC00 through U+DFFF).
返回
highSurrogate 和 lowSurrogate 参数表示的 21 位 Unicode 码位。The 21-bit Unicode code point represented by the highSurrogate and lowSurrogate parameters.
例外
highSurrogate 不在 U + D800 到 U + DBFF 的范围内,或者 lowSurrogate 不在 U + DC00 到 U+DFFF 的范围内。highSurrogate is not in the range U+D800 through U+DBFF, or lowSurrogate is not in the range U+DC00 through U+DFFF.
注解
使用此方法将代理项对转换为21位 Unicode 码位。Use this method to convert a surrogate pair into a 21-bit Unicode code point. 若要将 UTF-16 数据转换为32数据,请使用 System.Text.UTF32Encoding 类。To convert UTF-16 data into UTF-32 data, use the System.Text.UTF32Encoding class.
通常,UTF-16 编码表示一个 Unicode 字符作为16位代码单元。Ordinarily, UTF-16 encoding represents a single Unicode character as a 16-bit code unit. 但是,它还支持代理项对,这允许将单个抽象字符表示为 2 16 位代码单元。However, it also supports surrogate pairs, which allow a single abstract character to be represented as two 16-bit code units. 这两个 Char 对象的代码单位必须是从 u + D800 到 u + DBFF 的第一个 (高) 代理项和 u + DC00 到 u + DFFF 之间,第二个对象 (low) 代理项。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. 仅 UTF-16 编码支持代理项对。Surrogate pairs are supported only by UTF-16 encoding. 此方法允许将 UTF-16 代理项对所表示的字符转换为使用 32 UTF-16 编码的字符。This method allows a character represented by a UTF-16 surrogate pair to be converted to a character using UTF-32 encoding.
另请参阅
适用于
ConvertToUtf32(String, Int32)
将字符串中指定位置的 UTF-16 编码字符或代理项对的值转换为 Unicode 码位。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(System::String ^ s, int index);
public static int ConvertToUtf32 (string s, int index);
static member ConvertToUtf32 : string * int -> int
Public Shared Function ConvertToUtf32 (s As String, index As Integer) As Integer
参数
- s
- String
包含字符或代理项对的字符串。A string that contains a character or surrogate pair.
- index
- Int32
字符或代理项对在 s 中的索引位置。The index position of the character or surrogate pair in s.
返回
字符或代理项对表示的 21 位 Unicode 码位,该字符或代理项对在 s 参数中的位置由 index 参数指定。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.
例外
s 为 null。s is null.
index 不是 s 内的位置。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.
注解
使用此方法将字符或代理项对转换为21位 Unicode 码位。Use this method to convert a character or surrogate pair into a 21-bit Unicode code point. 若要将 UTF-16 数据转换为32数据,请使用 System.Text.UTF32Encoding 类。To convert UTF-16 data into UTF-32 data, use the System.Text.UTF32Encoding class.