次の方法で共有


ASCIIEncoding.GetChars メソッド (Byte[], Int32, Int32, Char , Int32)

バイト配列から指定した範囲の要素をデコードし、その結果を Unicode 文字配列内の指定した範囲の要素に格納します。

Overrides Overloads Public Function GetChars( _
   ByVal bytes() As Byte, _   ByVal byteIndex As Integer, _   ByVal byteCount As Integer, _   ByVal chars() As Char, _   ByVal charIndex As Integer _) As Integer
[C#]
public override int GetChars(byte[] bytes,intbyteIndex,intbyteCount,char[] chars,intcharIndex);
[C++]
public: int GetChars(unsigned charbytes __gc[],intbyteIndex,intbyteCount,__wchar_tchars __gc[],intcharIndex);
[JScript]
public override function GetChars(
   bytes : Byte[],byteIndex : int,byteCount : int,chars : Char[],charIndex : int) : int;

パラメータ

  • bytes
    デコードするバイト配列。
  • byteIndex
    デコードする bytes 内の最初の要素のインデックス。
  • byteCount
    デコードする要素の数。
  • chars
    デコードされた結果が格納される文字配列。
  • charIndex
    デコードされた結果が格納される chars 内の最初の要素のインデックス。

戻り値

chars に格納されている文字数。

例外

例外の種類 条件
ArgumentNullException bytes または chars が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException byteIndexbyteCount 、または charIndex が 0 未満です。

または

byteIndexbyteCount を加算した値が、 bytes の長さを超えています。

または

charIndexchars の長さを超えています。

ArgumentException chars の長さから charIndex を引いた値が byteCount 未満です。

解説

デコードする bytes の任意の要素で、16 進数 0x7F を超える値が、Unicode 疑問符 ('?') に変換されます。

デコードされたバイトを格納するために GetChars メソッドによって要求された配列サイズを、正確に計算するには GetCharCount を使用し、最大値を計算するには GetMaxCharCount を使用します。

使用例

[Visual Basic, C#, C++] バイト配列からある範囲の要素をデコードし、その結果を Unicode 文字配列の一連の要素に格納する方法を次の例に示します。 GetCharCount メソッドは、配列 bytes 内でデコードされた要素を格納するために必要な文字数を計算するときに使用します。 GetChars メソッドは、 bytes 内の指定された要素をデコードし、その結果を新しい文字配列 chars に格納します。

 
Imports System
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
             65,  83,  67,  73,  73,  32,  69, _
            110,  99, 111, 100, 105, 110, 103, _
             32,  69, 120,  97, 109, 112, 108, 101}

        Dim ascii As New ASCIIEncoding()

        Dim charCount As Integer = ascii.GetCharCount(bytes, 6, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = ascii.GetChars(bytes, 6, 8, chars, 0)

        Console.WriteLine("{0} characters used to decode bytes.", charsDecodedCount)

        Console.Write("Decoded chars: ")
        Dim c As Char
        For Each c In chars
            Console.Write("[{0}]", c)
        Next c
        Console.WriteLine()
    End Sub
End Class

[C#] 
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();

        int charCount = ascii.GetCharCount(bytes, 6, 8);
        chars = new Char[charCount];
        int charsDecodedCount = ascii.GetChars(bytes, 6, 8, chars, 0);

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);
        }
        Console.WriteLine();
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main() 
{
   Char chars[];
   Byte bytes[] = 
   {
      65,  83,  67,  73,  73,  32,  69,
      110,  99, 111, 100, 105, 110, 103,
      32,  69, 120,  97, 109, 112, 108, 101
   };

   ASCIIEncoding* ascii = new ASCIIEncoding();

   int charCount = ascii -> GetCharCount(bytes, 6, 8);
   chars = new Char[charCount];
   int charsDecodedCount = ascii -> GetChars(bytes, 6, 8, chars, 0);

   Console::WriteLine(S"{0} characters used to decode bytes.", __box(charsDecodedCount));

   Console::Write(S"Decoded chars: ");
   IEnumerator* myEnum = chars->GetEnumerator();
   while (myEnum->MoveNext()) 
   {
      Char* c = __try_cast<Char*>(myEnum->Current);
      Console::Write(S"[{0}]", c->ToString());
   }
   Console::WriteLine();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

ASCIIEncoding クラス | ASCIIEncoding メンバ | System.Text 名前空間 | ASCIIEncoding.GetChars オーバーロードの一覧 | GetCharCount | GetMaxCharCount | GetChars