Char.ConvertToUtf32 メソッド

定義

UTF-16 でエンコードされたサロゲート ペアの値を Unicode コード ポイントに変換します。

オーバーロード

ConvertToUtf32(Char, Char)

UTF-16 でエンコードされたサロゲート ペアの値を Unicode コード ポイントに変換します。

ConvertToUtf32(String, Int32)

文字列中の指定位置にある UTF-16 エンコード文字またはサロゲート ペアの値を、Unicode コード ポイントに変換します。

メソッドと メソッドを次のコード ConvertToUtf32ConvertFromUtf32 に示します。

// 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

*/
open System

let show (s: string) =
    for x = 0 to s.Length - 1 do
        printf $"""0x{int s[x]:X}{if x = s.Length - 1 then String.Empty else ", "}"""                    

[<EntryPoint>]
let main _ =
    let letterA = 0x0041  //U+00041 = LATIN CAPITAL LETTER A
    let music   = 0x1D161 //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
    let comment   = "Create a UTF-16 encoded string from a code point."
    let comment1b = "Create a code point from a UTF-16 encoded string."
    let comment2b = "Create a code point from a surrogate pair at a certain position in a string."
    let 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.

    printfn $"{comment}"
    let s1 = Char.ConvertFromUtf32 letterA
    printf $"    1a) 0x{letterA:X} => "
    show s1
    printfn ""

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

    printfn $"{comment1b}"
    let letterA = Char.ConvertToUtf32(s1, 0)
    printf "    1b) "
    show s1
    printfn $" => 0x{letterA:X}"
    printfn ""

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

//  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.

    printfn $"{comment}"
    let s1 = Char.ConvertFromUtf32 music
    printf $"    2a) 0x{music:X} => "
    show s1
    printfn ""

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

    printfn $"{comment2b}" 
    let music = Char.ConvertToUtf32(s1, 0)
    printf "    2b) "
    show s1
    printfn $" => 0x{music:X}"

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

    printfn $"{comment2c}"
    let music = Char.ConvertToUtf32(s1[0], s1[1])
    printf "    2c) "
    show s1
    printfn $" => 0x{music:X}"

    0

// 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 コード ポイントに変換します。

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 までのコード単位)。

lowSurrogate
Char

下位サロゲートのコード単位 (U+DC00 から U+DFFF までのコード単位)。

戻り値

Int32

highSurrogate パラメーターおよび lowSurrogate パラメーターによって表される 21 ビットの Unicode コード ポイント。

例外

highSurrogate が U+D800 から U+DBFF までの範囲内にないか、または lowSurrogate が U+DC00 から U+DFFF までの範囲内にありません。

注釈

サロゲート ペアを 21 ビット Unicode コード ポイントに変換するには、このメソッドを使用します。 UTF-16 データを UTF-32 データに変換するには、 クラスを使用 System.Text.UTF32Encoding します。

通常、UTF-16 エンコードは、1 つの Unicode 文字を 16 ビットのコード単位として表します。 ただし、1 つの抽象文字を 2 つの 16 ビット コード単位として表すサロゲート ペアもサポートしています。 これら 2 つのオブジェクトには、最初の (高い) サロゲートの場合は U+D800 から U+DBFF、2 番目 (低) サロゲートの場合は Char U+DC00 から U+DFFF の範囲のコード単位が必要です。 サロゲート ペアは、UTF-16 エンコードでのみサポートされます。 このメソッドを使用すると、UTF-16 サロゲート ペアで表される文字を UTF-32 エンコードを使用して文字に変換できます。

こちらもご覧ください

適用対象

ConvertToUtf32(String, Int32)

文字列中の指定位置にある UTF-16 エンコード文字またはサロゲート ペアの値を、Unicode コード ポイントに変換します。

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

文字またはサロゲート ペアを含む文字列。

index
Int32

s 内の文字またはサロゲート ペアのインデックス位置。

戻り値

Int32

s パラメーター内の、index パラメーターによって指定された位置にある文字またはサロゲート ペアが表す、21 ビットの Unicode コード ポイント。

例外

snullです。

indexs 内の位置ではありません。

指定されたインデックスの位置には、サロゲート ペアが含まれています。ペアの最初の文字が有効な上位サロゲートではないか、または 2 番目の文字が有効な下位サロゲートではありません。

注釈

文字またはサロゲート ペアを 21 ビット Unicode コード ポイントに変換するには、このメソッドを使用します。 UTF-16 データを UTF-32 データに変換するには、 クラスを使用 System.Text.UTF32Encoding します。

こちらもご覧ください

適用対象