Language

Encoding.GetChars メソッド

定義

派生クラスでオーバーライドされると、バイトシーケンスを一連の文字にデコードします。

オーバーロード

名前 説明
GetChars(Byte[], Int32, Int32, Char[], Int32)

派生クラスでオーバーライドされると、指定したバイト配列から指定した文字配列にバイトシーケンスをデコードします。

GetChars(Byte*, Int32, Char*, Int32)

派生クラスでオーバーライドされると、指定したバイト ポインターから始まるバイトシーケンスを、指定した文字ポインターから始めて格納される文字のセットにデコードします。

GetChars(Byte[], Int32, Int32)

派生クラスでオーバーライドされると、指定したバイト配列のバイト シーケンスを一連の文字にデコードします。

GetChars(ReadOnlySpan<Byte>, Span<Char>)

派生クラスでオーバーライドされると、指定した読み取り専用バイト スパン内のすべてのバイトを文字スパンにデコードします。

GetChars(Byte[])

派生クラスでオーバーライドされると、指定したバイト配列内のすべてのバイトを一連の文字にデコードします。

GetChars(Byte[], Int32, Int32, Char[], Int32)

ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs

派生クラスでオーバーライドされると、指定したバイト配列から指定した文字配列にバイトシーケンスをデコードします。

public:
 abstract int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
abstract member GetChars : byte[] * int * int * char[] * int -> int
Public MustOverride Function GetChars (bytes As Byte(), byteIndex As Integer, byteCount As Integer, chars As Char(), charIndex As Integer) As Integer

パラメーター

bytes
Byte[]

デコードするバイトシーケンスを含むバイト配列。

byteIndex
Int32

デコードする最初のバイトのインデックス。

byteCount
Int32

デコードするバイト数。

chars
Char[]

結果の文字セットを格納する文字配列。

charIndex
Int32

結果の文字セットの書き込みを開始する位置のインデックス。

返品

charsに書き込まれた実際の文字数。

例外

bytes は nullです。

-又は-

chars は nullです。

byteIndex または byteCount または charIndex が 0 未満です。

-又は-

byteIndex および byteCount は、 bytes内の有効な範囲を示していません。

-又は-

charIndex は、 charsの有効なインデックスではありません。

chars は、 charIndex から配列の末尾まで、結果の文字を格納するのに十分な容量がありません。

フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)

および

DecoderFallback は DecoderExceptionFallback に設定されます。

例

次の例では、あるエンコードから別のエンコードに文字列を変換します。

using System;
using System.Text;

class ConvertExample
{
   static void Run()
   {
      string unicodeString = "This string contains the unicode character Pi (\u03a0)";

      // Create two different encodings.
      Encoding ascii = Encoding.ASCII;
      Encoding unicode = Encoding.Unicode;

      // Convert the string into a byte array.
      byte[] unicodeBytes = unicode.GetBytes(unicodeString);

      // Perform the conversion from one encoding to the other.
      byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
         
      // Convert the new byte[] into a char[] and then into a string.
      char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
      ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
      string asciiString = new string(asciiChars);

      // Display the strings created before and after the conversion.
      Console.WriteLine("Original string: {0}", unicodeString);
      Console.WriteLine("Ascii converted string: {0}", asciiString);
   }
}

// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)
Imports System.Text

Class Example1
   Shared Sub Run()
      Dim unicodeString As String = "This string contains the unicode character Pi (" & ChrW(&H03A0) & ")"

      ' Create two different encodings.
      Dim ascii As Encoding = Encoding.ASCII
      Dim unicode As Encoding = Encoding.Unicode

      ' Convert the string into a byte array.
      Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)

      ' Perform the conversion from one encoding to the other.
      Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)

      ' Convert the new byte array into a char array and then into a string.
      Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
      ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
      Dim asciiString As New String(asciiChars)

      ' Display the strings created before and after the conversion.
      Console.WriteLine("Original string: {0}", unicodeString)
      Console.WriteLine("Ascii converted string: {0}", asciiString)
   End Sub
End Class
' The example displays the following output:
'    Original string: This string contains the unicode character Pi (Π)
'    Ascii converted string: This string contains the unicode character Pi (?)

次の例では、文字列をバイト配列にエンコードし、バイトの範囲を文字の配列にデコードします。

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Encoding u32LE = Encoding.GetEncoding( "utf-32" );
      Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );

      // Use a string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string using the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
      u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );

      // Encode the string using the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
      u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );

      // Get the char counts, decode eight bytes starting at index 0,
      // and print out the counts and the resulting bytes.
      Console.Write( "BE array with BE encoding : " );
      PrintCountsAndChars( barrBE, 0, 8, u32BE );
      Console.Write( "LE array with LE encoding : " );
      PrintCountsAndChars( barrLE, 0, 8, u32LE );
   }

   public static void PrintCountsAndChars( byte[] bytes, int index, int count, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-25} :", enc.ToString() );

      // Display the exact character count.
      int iCC  = enc.GetCharCount( bytes, index, count );
      Console.Write( " {0,-3}", iCC );

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount( count );
      Console.Write( " {0,-3} :", iMCC );

      // Decode the bytes and display the characters.
      char[] chars = enc.GetChars( bytes, index, count );

      // The following is an alternative way to decode the bytes:
      // char[] chars = new char[iCC];
      // enc.GetChars( bytes, index, count, chars, 0 );

      Console.WriteLine( chars );
   }
}


/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

*/
Imports System.Text

Public Class SamplesEncoding   

   Public Shared Sub Main()

      ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Dim u32LE As Encoding = Encoding.GetEncoding("utf-32")
      Dim u32BE As Encoding = Encoding.GetEncoding("utf-32BE")

      ' Use a string containing the following characters:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)

      ' Encode the string using the big-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates barrBE with the exact number of elements required.
      Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
      u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)

      ' Encode the string using the little-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates barrLE with the exact number of elements required.
      Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
      u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)

      ' Get the char counts, decode eight bytes starting at index 0,
      ' and print out the counts and the resulting bytes.
      Console.Write("BE array with BE encoding : ")
      PrintCountsAndChars(barrBE, 0, 8, u32BE)
      Console.Write("LE array with LE encoding : ")
      PrintCountsAndChars(barrLE, 0, 8, u32LE)

   End Sub


   Public Shared Sub PrintCountsAndChars(bytes() As Byte, index As Integer, count As Integer, enc As Encoding)

      ' Display the name of the encoding used.
      Console.Write("{0,-25} :", enc.ToString())

      ' Display the exact character count.
      Dim iCC As Integer = enc.GetCharCount(bytes, index, count)
      Console.Write(" {0,-3}", iCC)

      ' Display the maximum character count.
      Dim iMCC As Integer = enc.GetMaxCharCount(count)
      Console.Write(" {0,-3} :", iMCC)

      ' Decode the bytes.
      Dim chars As Char() = enc.GetChars(bytes, index, count)

      ' The following is an alternative way to decode the bytes:
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates the array with the exact number of elements required.
      ' Dim chars(iCC - 1) As Char
      ' enc.GetChars( bytes, index, count, chars, 0 )

      ' Display the characters.
      Console.WriteLine(chars)

   End Sub

End Class


'This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.
'
'BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
'LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

注釈

結果の文字を格納するために GetChars に必要な正確な配列サイズを計算するには、 GetCharCount メソッドを使用する必要があります。 配列の最大サイズを計算するには、 GetMaxCharCount メソッドを使用します。 一般に、 GetCharCount メソッドではメモリの割り当てが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般的に速くなります。

GetChars(Byte[], Int32, Int32, Char[], Int32) は、入力バイト シーケンスから文字を取得します。 Encoding.GetChars は Decoder.GetChars とは異なります。 Encoding は個別の変換を想定していますが、 Decoder は 1 つの入力ストリームで複数のパス用に設計されているためです。

変換するデータが連続したブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスの Decoder メソッドまたは Encoder メソッドによって提供されるGetDecoderまたはGetEncoderをそれぞれ使用する必要があります。

Note

このメソッドは、バイト配列などの任意のバイナリ データではなく、Unicode 文字を操作することを目的としています。 任意のバイナリ データをテキストにエンコードする必要がある場合は、 Convert.ToBase64CharArray などのメソッドによって実装される uuencode などのプロトコルを使用する必要があります。

GetCharCount メソッドは、バイト シーケンスをデコードする結果の文字数を決定し、GetChars メソッドは実際のデコードを実行します。 Encoding.GetChars メソッドは、単一の入力ストリームで複数のパスを処理するDecoder.GetCharsメソッドとは対照的に、個別の変換を想定しています。

複数のバージョンの GetCharCount と GetChars がサポートされています。 これらのメソッドの使用に関するプログラミング上の考慮事項を次に示します。

  • アプリでは、コード ページから複数の入力バイトをデコードし、複数の呼び出しを使用してバイトを処理する必要がある場合があります。 この場合、バッチで処理するとバイト シーケンスが中断される可能性があるため、呼び出し間の状態を維持する必要があります。 (たとえば、ISO-2022 シフト シーケンスの一部は、1 つの GetChars 呼び出しを終了し、次の GetChars 呼び出しの開始時に続行できます。 Encoding.GetChars は、不完全なシーケンスのフォールバックを呼び出しますが、 Decoder は次の呼び出しのためにそれらのシーケンスを記憶します)。

  • アプリが文字列出力を処理する場合は、 GetString メソッドをお勧めします。 このメソッドは文字列の長さをチェックしてバッファーを割り当てる必要があるため、少し遅くなりますが、結果として得られる String 型が推奨されます。

  • GetChars(Byte*, Int32, Char*, Int32)のバイト バージョンでは、特に大きなバッファーを複数呼び出す場合に、いくつかの高速な手法が可能になります。 ただし、ポインターが必要なため、このメソッドバージョンは安全でない場合があることに注意してください。

  • アプリで大量のデータを変換する必要がある場合は、出力バッファーを再利用する必要があります。 この場合、出力文字バッファーをサポートする GetChars(Byte[], Int32, Int32, Char[], Int32) バージョンが最適な選択肢です。

  • Decoder.Convertの代わりにGetCharCountメソッドを使用することを検討してください。 変換メソッドは可能な限り多くのデータを変換し、出力バッファーが小さすぎる場合は例外をスローします。 ストリームの連続デコードでは、多くの場合、このメソッドが最適な選択肢です。

こちらもご覧ください

適用対象

GetChars(Byte*, Int32, Char*, Int32)

ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs

重要

この API は CLS 準拠ではありません。

派生クラスでオーバーライドされると、指定したバイト ポインターから始まるバイトシーケンスを、指定した文字ポインターから始めて格納される文字のセットにデコードします。

public:
 virtual int GetChars(System::Byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
public virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int

パラメーター

bytes
Byte*

デコードする最初のバイトへのポインター。

byteCount
Int32

デコードするバイト数。

chars
Char*

結果の文字セットの書き込みを開始する位置へのポインター。

charCount
Int32

書き込む最大文字数。

返品

chars パラメーターによって示される位置に書き込まれた実際の文字数。

属性

例外

bytes は nullです。

-又は-

chars は nullです。

byteCount または charCount が 0 未満です。

charCount は、結果の文字数よりも少なくなります。

フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)

および

DecoderFallback は DecoderExceptionFallback に設定されます。

注釈

結果の文字を格納するために必要 GetChars 正確な配列サイズを計算するには、 GetCharCount メソッドを使用する必要があります。 配列の最大サイズを計算するには、 GetMaxCharCount メソッドを使用します。 一般に、 GetCharCount メソッドではメモリの割り当てが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般的に速くなります。

Encoding.GetChars は、入力バイト シーケンスから文字を取得します。 Encoding.GetChars は Decoder.GetChars とは異なります。 Encoding は個別の変換を想定していますが、 Decoder は 1 つの入力ストリームで複数のパス用に設計されているためです。

変換するデータがシーケンシャル ブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスのDecoderまたはEncoder メソッドによって提供されるGetDecoderまたはGetEncoder オブジェクトをそれぞれ使用する必要があります。

Note

このメソッドは、バイト配列などの任意のバイナリ データではなく、Unicode 文字を操作することを目的としています。 任意のバイナリ データをテキストにエンコードする必要がある場合は、 Convert.ToBase64CharArray などのメソッドによって実装される uuencode などのプロトコルを使用する必要があります。

GetCharCount メソッドは、バイト シーケンスをデコードする結果の文字数を決定し、GetChars メソッドは実際のデコードを実行します。 Encoding.GetChars メソッドは、単一の入力ストリームで複数のパスを処理するDecoder.GetCharsメソッドとは対照的に、個別の変換を想定しています。

複数のバージョンの GetCharCount と GetChars がサポートされています。 これらのメソッドの使用に関するプログラミング上の考慮事項を次に示します。

  • アプリでは、コード ページから複数の入力バイトをデコードし、複数の呼び出しを使用してバイトを処理する必要がある場合があります。 この場合、バッチで処理するとバイト シーケンスが中断される可能性があるため、呼び出し間の状態を維持する必要があります。 (たとえば、ISO-2022 シフト シーケンスの一部は、1 つの GetChars 呼び出しを終了し、次の GetChars 呼び出しの開始時に続行できます。 Encoding.GetChars は、不完全なシーケンスのフォールバックを呼び出しますが、 Decoder は次の呼び出しのためにそれらのシーケンスを記憶します)。

  • アプリが文字列出力を処理する場合は、 GetString メソッドをお勧めします。 このメソッドは文字列の長さをチェックしてバッファーを割り当てる必要があるため、少し遅くなりますが、結果として得られる String 型が推奨されます。

  • GetChars(Byte*, Int32, Char*, Int32)のバイト バージョンでは、特に大きなバッファーを複数呼び出す場合に、いくつかの高速な手法が可能になります。 ただし、ポインターが必要なため、このメソッドバージョンは安全でない場合があることに注意してください。

  • アプリで大量のデータを変換する必要がある場合は、出力バッファーを再利用する必要があります。 この場合、出力文字バッファーをサポートする GetChars(Byte[], Int32, Int32, Char[], Int32) バージョンが最適な選択肢です。

  • Decoder.Convertの代わりにGetCharCountメソッドを使用することを検討してください。 変換メソッドは可能な限り多くのデータを変換し、出力バッファーが小さすぎる場合は例外をスローします。 ストリームの連続デコードでは、多くの場合、このメソッドが最適な選択肢です。

こちらもご覧ください

適用対象

GetChars(Byte[], Int32, Int32)

ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs

派生クラスでオーバーライドされると、指定したバイト配列のバイト シーケンスを一連の文字にデコードします。

public:
 virtual cli::array <char> ^ GetChars(cli::array <System::Byte> ^ bytes, int index, int count);
public virtual char[] GetChars(byte[] bytes, int index, int count);
abstract member GetChars : byte[] * int * int -> char[]
override this.GetChars : byte[] * int * int -> char[]
Public Overridable Function GetChars (bytes As Byte(), index As Integer, count As Integer) As Char()

パラメーター

bytes
Byte[]

デコードするバイトシーケンスを含むバイト配列。

index
Int32

デコードする最初のバイトのインデックス。

count
Int32

デコードするバイト数。

返品

Char[]

指定したバイト シーケンスをデコードした結果を含む文字配列。

例外

bytes は nullです。

index または count が 0 未満です。

-又は-

index および count は、 bytes内の有効な範囲を示していません。

フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)

および

DecoderFallback は DecoderExceptionFallback に設定されます。

例

次の例では、文字列をバイト配列にエンコードし、バイトの範囲を文字の配列にデコードします。

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Encoding u32LE = Encoding.GetEncoding( "utf-32" );
      Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );

      // Use a string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string using the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
      u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );

      // Encode the string using the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
      u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );

      // Get the char counts, decode eight bytes starting at index 0,
      // and print out the counts and the resulting bytes.
      Console.Write( "BE array with BE encoding : " );
      PrintCountsAndChars( barrBE, 0, 8, u32BE );
      Console.Write( "LE array with LE encoding : " );
      PrintCountsAndChars( barrLE, 0, 8, u32LE );
   }

   public static void PrintCountsAndChars( byte[] bytes, int index, int count, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-25} :", enc.ToString() );

      // Display the exact character count.
      int iCC  = enc.GetCharCount( bytes, index, count );
      Console.Write( " {0,-3}", iCC );

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount( count );
      Console.Write( " {0,-3} :", iMCC );

      // Decode the bytes and display the characters.
      char[] chars = enc.GetChars( bytes, index, count );

      // The following is an alternative way to decode the bytes:
      // char[] chars = new char[iCC];
      // enc.GetChars( bytes, index, count, chars, 0 );

      Console.WriteLine( chars );
   }
}


/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

*/
Imports System.Text

Public Class SamplesEncoding   

   Public Shared Sub Main()

      ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Dim u32LE As Encoding = Encoding.GetEncoding("utf-32")
      Dim u32BE As Encoding = Encoding.GetEncoding("utf-32BE")

      ' Use a string containing the following characters:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)

      ' Encode the string using the big-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates barrBE with the exact number of elements required.
      Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
      u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)

      ' Encode the string using the little-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates barrLE with the exact number of elements required.
      Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
      u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)

      ' Get the char counts, decode eight bytes starting at index 0,
      ' and print out the counts and the resulting bytes.
      Console.Write("BE array with BE encoding : ")
      PrintCountsAndChars(barrBE, 0, 8, u32BE)
      Console.Write("LE array with LE encoding : ")
      PrintCountsAndChars(barrLE, 0, 8, u32LE)

   End Sub


   Public Shared Sub PrintCountsAndChars(bytes() As Byte, index As Integer, count As Integer, enc As Encoding)

      ' Display the name of the encoding used.
      Console.Write("{0,-25} :", enc.ToString())

      ' Display the exact character count.
      Dim iCC As Integer = enc.GetCharCount(bytes, index, count)
      Console.Write(" {0,-3}", iCC)

      ' Display the maximum character count.
      Dim iMCC As Integer = enc.GetMaxCharCount(count)
      Console.Write(" {0,-3} :", iMCC)

      ' Decode the bytes.
      Dim chars As Char() = enc.GetChars(bytes, index, count)

      ' The following is an alternative way to decode the bytes:
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates the array with the exact number of elements required.
      ' Dim chars(iCC - 1) As Char
      ' enc.GetChars( bytes, index, count, chars, 0 )

      ' Display the characters.
      Console.WriteLine(chars)

   End Sub

End Class


'This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.
'
'BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
'LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

注釈

Encoding.GetChars は、入力バイト シーケンスから文字を取得します。 Encoding.GetChars は Decoder.GetChars とは異なります。 Encoding は個別の変換を想定していますが、 Decoder は 1 つの入力ストリームで複数のパス用に設計されているためです。

変換するデータが連続したブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスの Decoder メソッドまたは Encoder メソッドによって提供されるGetDecoderまたはGetEncoderをそれぞれ使用する必要があります。

Note

このメソッドは、バイト配列などの任意のバイナリ データではなく、Unicode 文字を操作することを目的としています。 任意のバイナリ データをテキストにエンコードする必要がある場合は、 Convert.ToBase64CharArray などのメソッドによって実装される uuencode などのプロトコルを使用する必要があります。

GetCharCount メソッドは、バイト シーケンスをデコードする結果の文字数を決定し、GetChars メソッドは実際のデコードを実行します。 Encoding.GetChars メソッドは、単一の入力ストリームで複数のパスを処理するDecoder.GetCharsメソッドとは対照的に、個別の変換を想定しています。

複数のバージョンの GetCharCount と GetChars がサポートされています。 これらのメソッドの使用に関するプログラミング上の考慮事項を次に示します。

  • アプリでは、コード ページから複数の入力バイトをデコードし、複数の呼び出しを使用してバイトを処理する必要がある場合があります。 この場合、バッチで処理するとバイト シーケンスが中断される可能性があるため、呼び出し間の状態を維持する必要があります。 (たとえば、ISO-2022 シフト シーケンスの一部は、1 つの GetChars 呼び出しを終了し、次の GetChars 呼び出しの開始時に続行できます。 Encoding.GetChars は、不完全なシーケンスのフォールバックを呼び出しますが、 Decoder は次の呼び出しのためにそれらのシーケンスを記憶します)。

  • アプリが文字列出力を処理する場合は、 GetString メソッドを使用することをお勧めします。 このメソッドは文字列の長さをチェックしてバッファーを割り当てる必要があるため、少し遅くなりますが、結果として得られる String 型が推奨されます。

  • GetChars(Byte*, Int32, Char*, Int32)のバイト バージョンでは、特に大きなバッファーを複数呼び出す場合に、いくつかの高速な手法が可能になります。 ただし、ポインターが必要なため、このメソッドバージョンは安全でない場合があることに注意してください。

  • アプリで大量のデータを変換する必要がある場合は、出力バッファーを再利用する必要があります。 この場合、出力文字バッファーをサポートする GetChars(Byte[], Int32, Int32, Char[], Int32) バージョンが最適な選択肢です。

  • Decoder.Convertの代わりにGetCharCountメソッドを使用することを検討してください。 変換メソッドは可能な限り多くのデータを変換し、出力バッファーが小さすぎる場合は例外をスローします。 ストリームの連続デコードでは、多くの場合、このメソッドが最適な選択肢です。

こちらもご覧ください

適用対象

GetChars(ReadOnlySpan<Byte>, Span<Char>)

ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs

派生クラスでオーバーライドされると、指定した読み取り専用バイト スパン内のすべてのバイトを文字スパンにデコードします。

public:
 virtual int GetChars(ReadOnlySpan<System::Byte> bytes, Span<char> chars);
public virtual int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars);
abstract member GetChars : ReadOnlySpan<byte> * Span<char> -> int
override this.GetChars : ReadOnlySpan<byte> * Span<char> -> int
Public Overridable Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char)) As Integer

パラメーター

bytes
ReadOnlySpan<Byte>

デコードするバイトシーケンスを含む読み取り専用スパン。

chars
Span<Char>

デコードされたバイトを受信する文字スパン。

返品

chars パラメーターによって示されるスパンで書き込まれた実際の文字数。

注釈

Encoding.GetChars は、入力バイト スパンから文字を取得します。 Encoding.GetChars は Decoder.GetChars とは異なります。 Encoding は個別の変換を想定していますが、 Decoder は 1 つの入力ストリームで複数のパス用に設計されているためです。

変換するデータが連続したブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスの Decoder メソッドまたは Encoder メソッドによって提供されるGetDecoderまたはGetEncoderをそれぞれ使用する必要があります。

GetCharCount メソッドは、バイト シーケンスをデコードする結果の文字数を決定し、GetChars メソッドは実際のデコードを実行します。 Encoding.GetChars メソッドは、単一の入力ストリームで複数のパスを処理するDecoder.GetCharsメソッドとは対照的に、個別の変換を想定しています。

複数のバージョンの GetCharCount と GetChars がサポートされています。 これらのメソッドの使用に関するプログラミング上の考慮事項を次に示します。

  • アプリでは、コード ページから複数の入力バイトをデコードし、複数の呼び出しを使用してバイトを処理する必要がある場合があります。 この場合、バッチで処理するとバイト シーケンスが中断される可能性があるため、呼び出し間の状態を維持する必要があります。 (たとえば、ISO-2022 シフト シーケンスの一部は、1 つの GetChars 呼び出しを終了し、次の GetChars 呼び出しの開始時に続行できます。 Encoding.GetChars は、不完全なシーケンスのフォールバックを呼び出しますが、 Decoder は次の呼び出しのためにそれらのシーケンスを記憶します)。

  • アプリが文字列出力を処理する場合は、 GetString メソッドを使用することをお勧めします。 このメソッドは文字列の長さをチェックしてバッファーを割り当てる必要があるため、少し遅くなりますが、結果として得られる String 型が推奨されます。

  • GetChars(Byte*, Int32, Char*, Int32)のバイト バージョンでは、特に大きなバッファーを複数呼び出す場合に、いくつかの高速な手法が可能になります。 ただし、ポインターが必要なため、このメソッドバージョンは安全でない場合があることに注意してください。

  • アプリで大量のデータを変換する必要がある場合は、出力バッファーを再利用する必要があります。 この場合、出力文字バッファーをサポートする GetChars(Byte[], Int32, Int32, Char[], Int32) バージョンが最適な選択肢です。

  • Decoder.Convertの代わりにGetCharCountメソッドを使用することを検討してください。 変換メソッドは可能な限り多くのデータを変換し、出力バッファーが小さすぎる場合は例外をスローします。 ストリームの連続デコードでは、多くの場合、このメソッドが最適な選択肢です。

適用対象

GetChars(Byte[])

ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs
ソース:
Encoding.cs

派生クラスでオーバーライドされると、指定したバイト配列内のすべてのバイトを一連の文字にデコードします。

public:
 virtual cli::array <char> ^ GetChars(cli::array <System::Byte> ^ bytes);
public virtual char[] GetChars(byte[] bytes);
abstract member GetChars : byte[] -> char[]
override this.GetChars : byte[] -> char[]
Public Overridable Function GetChars (bytes As Byte()) As Char()

パラメーター

bytes
Byte[]

デコードするバイトシーケンスを含むバイト配列。

返品

Char[]

指定したバイト シーケンスをデコードした結果を含む文字配列。

例外

bytes は nullです。

フォールバックが発生しました (詳細については、「 .NET での文字エンコード」を参照してください)

および

DecoderFallback は DecoderExceptionFallback に設定されます。

例

次の例では、文字列をバイト配列にエンコードし、バイトを文字の配列にデコードします。

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Encoding u32LE = Encoding.GetEncoding( "utf-32" );
      Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );

      // Use a string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string using the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
      u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );

      // Encode the string using the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
      u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );

      // Get the char counts, and decode the byte arrays.
      Console.Write( "BE array with BE encoding : " );
      PrintCountsAndChars( barrBE, u32BE );
      Console.Write( "LE array with LE encoding : " );
      PrintCountsAndChars( barrLE, u32LE );
   }

   public static void PrintCountsAndChars( byte[] bytes, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-25} :", enc.ToString() );

      // Display the exact character count.
      int iCC  = enc.GetCharCount( bytes );
      Console.Write( " {0,-3}", iCC );

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount( bytes.Length );
      Console.Write( " {0,-3} :", iMCC );

      // Decode the bytes and display the characters.
      char[] chars = enc.GetChars( bytes );
      Console.WriteLine( chars );
   }
}


/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

BE array with BE encoding : System.Text.UTF32Encoding : 5   12  :zăǽβ
LE array with LE encoding : System.Text.UTF32Encoding : 5   12  :zăǽβ

*/
Imports System.Text

Public Class SamplesEncoding   

   Public Shared Sub Main()

      ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Dim u32LE As Encoding = Encoding.GetEncoding("utf-32")
      Dim u32BE As Encoding = Encoding.GetEncoding("utf-32BE")

      ' Use a string containing the following characters:
      '    Latin Small Letter Z (U+007A)
      '    Latin Small Letter A (U+0061)
      '    Combining Breve (U+0306)
      '    Latin Small Letter AE With Acute (U+01FD)
      '    Greek Small Letter Beta (U+03B2)
      Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) 

      ' Encode the string using the big-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates the array with the exact number of elements required.
      Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
      u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)

      ' Encode the string using the little-endian byte order.
      ' NOTE: In VB.NET, arrays contain one extra element by default.
      '       The following line creates the array with the exact number of elements required.
      Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
      u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)

      ' Get the char counts, and decode the byte arrays.
      Console.Write("BE array with BE encoding : ")
      PrintCountsAndChars(barrBE, u32BE)
      Console.Write("LE array with LE encoding : ")
      PrintCountsAndChars(barrLE, u32LE)

   End Sub


   Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)

      ' Display the name of the encoding used.
      Console.Write("{0,-25} :", enc.ToString())

      ' Display the exact character count.
      Dim iCC As Integer = enc.GetCharCount(bytes)
      Console.Write(" {0,-3}", iCC)

      ' Display the maximum character count.
      Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
      Console.Write(" {0,-3} :", iMCC)

      ' Decode the bytes and display the characters.
      Dim chars As Char() = enc.GetChars(bytes)
      Console.WriteLine(chars)

   End Sub

End Class


'This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.
'
'BE array with BE encoding : System.Text.UTF32Encoding : 5   12  :zăǽβ
'LE array with LE encoding : System.Text.UTF32Encoding : 5   12  :zăǽβ

注釈

Encoding.GetChars は、入力バイト シーケンスから文字を取得します。 Encoding.GetChars は Decoder.GetChars とは異なります。 Encoding は個別の変換を想定していますが、 Decoder は 1 つの入力ストリームで複数のパス用に設計されているためです。

変換するデータが連続したブロックでのみ使用できる場合 (ストリームから読み取られるデータなど)、またはデータの量が非常に大きくて小さいブロックに分割する必要がある場合は、派生クラスの Decoder メソッドまたは Encoder メソッドによって提供されるGetDecoderまたはGetEncoderをそれぞれ使用する必要があります。

Note

このメソッドは、バイト配列などの任意のバイナリ データではなく、Unicode 文字を操作することを目的としています。 任意のバイナリ データをテキストにエンコードする必要がある場合は、 Convert.ToBase64CharArray などのメソッドによって実装される uuencode などのプロトコルを使用する必要があります。

GetCharCount メソッドは、バイト シーケンスをデコードする結果の文字数を決定し、GetChars メソッドは実際のデコードを実行します。 Encoding.GetChars メソッドは、単一の入力ストリームで複数のパスを処理するDecoder.GetCharsメソッドとは対照的に、個別の変換を想定しています。

複数のバージョンの GetCharCount と GetChars がサポートされています。 これらのメソッドの使用に関するプログラミング上の考慮事項を次に示します。

  • アプリでは、コード ページから複数の入力バイトをデコードし、複数の呼び出しを使用してバイトを処理する必要がある場合があります。 この場合、バッチで処理するとバイト シーケンスが中断される可能性があるため、呼び出し間の状態を維持する必要があります。 (たとえば、ISO-2022 シフト シーケンスの一部は、1 つの GetChars 呼び出しを終了し、次の GetChars 呼び出しの開始時に続行できます。 Encoding.GetChars は、不完全なシーケンスのフォールバックを呼び出しますが、 Decoder は次の呼び出しのためにそれらのシーケンスを記憶します)。

  • アプリが文字列出力を処理する場合は、 GetString メソッドを使用することをお勧めします。 このメソッドは文字列の長さをチェックしてバッファーを割り当てる必要があるため、少し遅くなりますが、結果として得られる String 型が推奨されます。

  • GetChars(Byte*, Int32, Char*, Int32)のバイト バージョンでは、特に大きなバッファーを複数呼び出す場合に、いくつかの高速な手法が可能になります。 ただし、ポインターが必要なため、このメソッドバージョンは安全でない場合があることに注意してください。

  • アプリで大量のデータを変換する必要がある場合は、出力バッファーを再利用する必要があります。 この場合、出力文字バッファーをサポートする GetChars(Byte[], Int32, Int32, Char[], Int32) バージョンが最適な選択肢です。

  • Decoder.Convertの代わりにGetCharCountメソッドを使用することを検討してください。 変換メソッドは可能な限り多くのデータを変換し、出力バッファーが小さすぎる場合は例外をスローします。 ストリームの連続デコードでは、多くの場合、このメソッドが最適な選択肢です。

こちらもご覧ください

適用対象