Language

UnicodeEncoding.GetString(Byte[], Int32, Int32) メソッド

定義

バイト配列から文字列にバイト範囲をデコードします。

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString(byte[] bytes, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override string GetString(byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String

パラメーター

bytes
Byte[]

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

index
Int32

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

count
Int32

デコードするバイト数。

返品

指定したバイト シーケンスをデコードした結果を格納する String オブジェクト。

属性

例外

bytes が null (Nothing) です。

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

-又は-

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

エラー検出が有効になっており、 bytes に無効なバイト シーケンスが含まれています。

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

および

DecoderFallback は DecoderExceptionFallback に設定されます。

例

次の例では、 GetByteCount メソッドを呼び出してエンコードされた文字列に必要なバイト数を正確に判断し、バイト オーダー マーク (BOM) のサイズを追加して配列を初期化します。 次に、 GetPreamble メソッドを呼び出して BOM を配列に格納してから、 GetBytes メソッドを呼び出して、エンコードされたバイトを配列に格納します。 次に、 GetString メソッドを呼び出して文字列をデコードします。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      UTF8Encoding utf8 = new UTF8Encoding(true, true);

      String s = "It was the best of times, it was the worst of times...";

      // We need to dimension the array, since we'll populate it with 2 method calls.
      Byte[] bytes = new Byte[utf8.GetByteCount(s) + utf8.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length);
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf8.GetString(bytes, 0, bytes.Length);
      Console.WriteLine(s2);
   }
}
// The example displays the following output:
//        ?It was the best of times, it was the worst of times...
Imports System.Text

Module Example
   Public Sub Main()
      Dim utf8 As New UTF8Encoding(True, True)

      Dim s As String = "It was the best of times, it was the worst of times..."

      ' We need to dimension the array, since we'll populate it with 2 method calls.
      Dim bytes(utf8.GetByteCount(s) + utf8.GetPreamble().Length - 1) As Byte
      ' Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length)
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length)

      ' Decode the byte array.
      Dim s2 As String = utf8.GetString(bytes, 0, bytes.Length)
      Console.WriteLine(s2)
   End Sub
End Module
' The example displays the following output:
'       ?It was the best of times, it was the worst of times...

この場合、デコードされた文字列は 16 ビットバイトオーダーマーク U+FFFD で始まるため、元の文字列とは異なっています。 つまり、2 つの文字列が等しくないと比較され、文字列が出力された場合、BOM は置換文字 "?"" として表示されます。 文字列の先頭にある BOM を削除するには、 String.TrimStart メソッドを呼び出します。

注釈

エラー検出では、無効なシーケンスにより、このメソッドは ArgumentExceptionをスローします。 エラー検出がないと、無効なシーケンスは無視され、例外はスローされません。

デコードするバイトの範囲にバイト オーダー マーク (BOM) が含まれ、バイト配列が BOM 非対応型のメソッドによって返された場合、このメソッドによって返される文字配列には、文字 U + FFFE が含まれます。 String.TrimStart メソッドを呼び出すことで削除できます。

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

適用対象

こちらもご覧ください