次の方法で共有


UnicodeEncoding.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 byteIndex, byteCount または charIndex が 0 未満です。

または

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

または

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

ArgumentException chars には、デコードされた文字を格納するために必要な領域がありません。

解説

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

使用例

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

 
Imports System
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = {85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0}
        
        Dim uni As New UnicodeEncoding()
        
        Dim charCount As Integer = uni.GetCharCount(bytes, 2, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = uni.GetChars(bytes, 2, 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 'Main
End Class 'UnicodeEncodingExample

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

class UnicodeEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        UnicodeEncoding Unicode = new UnicodeEncoding();

        int charCount = Unicode.GetCharCount(bytes, 2, 8);
        chars = new Char[charCount];
        int charsDecodedCount = Unicode.GetChars(bytes, 2, 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[] = 
   {
      85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
   };

   UnicodeEncoding* Unicode = new UnicodeEncoding();

   int charCount = Unicode -> GetCharCount(bytes, 2, 8);
   chars = new Char[charCount];
   int charsDecodedCount = Unicode -> GetChars(bytes, 2, 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

参照

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