次の方法で共有


UTF8Encoding.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 bytes には、無効なバイト シーケンスが含まれ、無効なバイトが検出された場合に例外をスローするように指定されたこのインスタンスの UTF8Encoding コンストラクタも含まれています。

または

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

解説

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

エラー検出がオフのときに、無効な UTF-8 バイト シーケンスが検出された場合、無効なバイトは無視され、 chars にエンコードされず、例外もスローされません。

使用例

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

 
Imports System
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 13)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = utf8.GetChars(bytes, 2, 13, 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 'UTF8EncodingExample

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

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();

        int charCount = utf8.GetCharCount(bytes, 2, 13);
        chars = new Char[charCount];
        int charsDecodedCount = utf8.GetChars(bytes, 2, 13, 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,  84,  70,  56,  32,  69, 110,
      99, 111, 100, 105, 110, 103,  32,
      69, 120,  97, 109, 112, 108, 101
   };

   UTF8Encoding* utf8 = new UTF8Encoding();

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

参照

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