UTF32Encoding.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);
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

指定したバイト シーケンスのデコード結果が格納されている文字列。

例外

bytesnullです。

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

または index および countbytes において有効な範囲を表していません。

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

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

および DecoderFallbackDecoderExceptionFallback に設定されます。

次の例では、文字列を 2 つのバイト配列にエンコードします。1 つはリトル エンディアン順で、もう 1 つはビッグ エンディアン順です。 次に、バイトを文字列にデコードします。

using System;
using System.Text;

public class Example  
{
   public static void Main()  
   {
      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      UTF32Encoding u32LE = new UTF32Encoding(false, true, true);
      UTF32Encoding u32BE = new UTF32Encoding(true, true, true);

      // Create byte arrays from the same 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 str = "za\u0306\u01FD\u03B2";

      // barrBE uses the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount(str)];
      u32BE.GetBytes(str, 0, str.Length, barrBE, 0);

      // barrLE uses the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount(str)];
      u32LE.GetBytes(str, 0, str.Length, barrLE, 0);

      // Decode the byte arrays.
      Console.WriteLine("BE array with BE encoding:");
      DisplayString(barrBE, u32BE);
      Console.WriteLine();

      Console.WriteLine("LE array with LE encoding:");
      DisplayString(barrLE, u32LE);
      Console.WriteLine();
   
      // Decode the byte arrays using an encoding with a different byte order.
      Console.WriteLine("BE array with LE encoding:");
      try  {
         DisplayString(barrBE, u32LE);
      }
      catch (System.ArgumentException e)  {
         Console.WriteLine(e.Message);
      }
      Console.WriteLine();

      Console.WriteLine("LE array with BE encoding:");
      try  {
         DisplayString(barrLE, u32BE);
      }
      catch (ArgumentException e)  {
         Console.WriteLine(e.Message);
      }
      Console.WriteLine();
   }

   public static void DisplayString(byte[] bytes, Encoding enc)  
   {
      // Display the name of the encoding used.
      Console.Write("{0,-25}: ", enc.ToString());

      // Decode the bytes and display the characters.
      Console.WriteLine(enc.GetString(bytes, 0, bytes.Length));
   }
}
// This example displays the following output:
//   BE array with BE encoding:
//   System.Text.UTF32Encoding: zăǽβ
//
//   LE array with LE encoding:
//   System.Text.UTF32Encoding: zăǽβ
//
//   BE array with LE encoding:
//   System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
//
//   LE array with BE encoding:
//   System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.
Imports System.Text

Public Module Example  
   Public Sub Main()  
      ' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Dim u32LE As New UTF32Encoding(False, True, True)
      Dim u32BE As New UTF32Encoding(True, True, True)

      ' Create byte arrays from the same 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 str As String = "za" + ChrW(&h0306) + ChrW(&h01FD) + ChrW(&h03B2)

      ' barrBE uses the big-endian byte order.
      Dim barrBE(u32BE.GetByteCount(str) - 1) As Byte
      u32BE.GetBytes(str, 0, str.Length, barrBE, 0)

      ' barrLE uses the little-endian byte order.
      Dim barrLE(u32LE.GetByteCount(str) - 1) As Byte
      u32LE.GetBytes(str, 0, str.Length, barrLE, 0)

      ' Decode the byte arrays.
      Console.WriteLine("BE array with BE encoding:")
      DisplayString(barrBE, u32BE)
      Console.WriteLine()

      Console.WriteLine("LE array with LE encoding:")
      DisplayString(barrLE, u32LE)
      Console.WriteLine()
   
      ' Decode the byte arrays using an encoding with a different byte order.
      Console.WriteLine("BE array with LE encoding:")
      Try  
         DisplayString(barrBE, u32LE)
      Catch e As ArgumentException
         Console.WriteLine(e.Message)
      End Try
      Console.WriteLine()

      Console.WriteLine("LE array with BE encoding:")
      Try  
         DisplayString(barrLE, u32BE)
      Catch e As ArgumentException
         Console.WriteLine(e.Message)
      End Try
      Console.WriteLine()
   End Sub

   Public Sub DisplayString(bytes As Byte(), enc As Encoding )  
      ' Display the name of the encoding used.
      Console.Write("{0,-25}: ", enc.ToString())

      ' Decode the bytes and display the characters.
      Console.WriteLine(enc.GetString(bytes, 0, bytes.Length))
   End Sub
End Module
' This example displays the following output:
'   BE array with BE encoding:
'   System.Text.UTF32Encoding: zăǽβ
'
'   LE array with LE encoding:
'   System.Text.UTF32Encoding: zăǽβ
'
'   BE array with LE encoding:
'   System.Text.UTF32Encoding: Unable to translate bytes [00][00][00][7A] at index 0 from specified code page to Unicode.
'
'   LE array with BE encoding:
'   System.Text.UTF32Encoding: Unable to translate bytes [7A][00][00][00] at index 0 from specified code page to Unicode.

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

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      var utf32 = new UTF32Encoding(! BitConverter.IsLittleEndian, 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[utf32.GetByteCount(s) + utf32.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length);
      utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf32.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 utf32 As New UTF32Encoding(Not BitConverter.IsLittleEndian, 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(utf32.GetByteCount(s) + utf32.GetPreamble().Length - 1) As Byte
      ' Encode the string.
      Array.Copy(utf32.GetPreamble(), bytes, utf32.GetPreamble().Length)
      utf32.GetBytes(s, 0, s.Length, bytes, utf32.GetPreamble().Length)

      ' Decode the byte array.
      Dim s2 As String = utf32.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...

この場合、デコードされた文字列は、32 ビットバイト順マーク U+FFFE U+0000 で始まるため、元の文字列とは異なっています。 つまり、2 つの文字列は等しくないものとして比較され、文字列が出力された場合、BOM は置換文字 "?"" として表示されます。

注釈

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

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

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

適用対象

こちらもご覧ください