UnicodeEncoding.GetChars メソッド

定義

バイトシーケンスを文字のセットにデコードします。

オーバーロード

名前 説明
GetChars(Byte*, Int32, Char*, Int32)

指定したバイト ポインターから始まるバイト シーケンスを、指定した文字ポインターから始めて格納される文字のセットにデコードします。

GetChars(Byte[], Int32, Int32, Char[], Int32)

指定したバイト配列から指定した文字配列にバイト シーケンスをデコードします。

GetChars(Byte*, Int32, Char*, Int32)

重要

この API は CLS 準拠ではありません。

指定したバイト ポインターから始まるバイト シーケンスを、指定した文字ポインターから始めて格納される文字のセットにデコードします。

public:
 override int GetChars(System::Byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public override int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[System.CLSCompliant(false)]
public override int GetChars(byte* bytes, int byteCount, char* chars, int charCount);
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int

パラメーター

bytes
Byte*

デコードする最初のバイトへのポインター。

byteCount
Int32

デコードするバイト数。

chars
Char*

結果の文字セットの書き込みを開始する位置へのポインター。

charCount
Int32

書き込む最大文字数。

返品

chars パラメーターによって示される位置に書き込まれた実際の文字数。

属性

例外

bytesnull (Nothing) です。

-または-

charsnull (Nothing) です。

byteCount または charCount が 0 未満です。

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

-または-

charCount は、結果の文字数よりも少なくなります。

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

および

DecoderFallbackDecoderExceptionFallback に設定されます。

注釈

結果の文字を格納するために必要 GetChars 正確な配列サイズを計算するために、アプリケーションは GetCharCountを使用します。 配列の最大サイズを計算するには、アプリケーションで GetMaxCharCountを使用する必要があります。 一般に、 GetCharCount メソッドは割り当てるメモリが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般に速くなります。

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

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

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

こちらもご覧ください

適用対象

GetChars(Byte[], Int32, Int32, Char[], Int32)

指定したバイト配列から指定した文字配列にバイト シーケンスをデコードします。

public:
 override int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
override this.GetChars : byte[] * int * int * char[] * int -> int
Public Overrides Function GetChars (bytes As Byte(), byteIndex As Integer, byteCount As Integer, chars As Char(), charIndex As Integer) As Integer

パラメーター

bytes
Byte[]

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

byteIndex
Int32

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

byteCount
Int32

デコードするバイト数。

chars
Char[]

結果の文字セットを格納する文字配列。

charIndex
Int32

結果の文字セットの書き込みを開始する位置のインデックス。

返品

charsに書き込まれた実際の文字数。

例外

bytesnull (Nothing) です。

-または-

charsnull (Nothing) です。

byteIndex または byteCount または charIndex が 0 未満です。

-または-

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

-または-

charIndex は、 charsの有効なインデックスではありません。

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

-または-

chars は、 charIndex から配列の末尾まで、結果の文字を格納するのに十分な容量がありません。

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

および

DecoderFallbackDecoderExceptionFallback に設定されます。

次の例では、 GetChars メソッドを使用してバイト配列内の要素の範囲をデコードし、結果を文字配列に格納する方法を示します。

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();
    }
}
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
End Class

注釈

結果の文字を格納するために GetChars に必要な正確な配列サイズを計算するために、アプリケーションは GetCharCountを使用します。 配列の最大サイズを計算するには、アプリケーションで GetMaxCharCountを使用する必要があります。 一般に、 GetCharCount メソッドは割り当てるメモリが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般に速くなります。

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

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

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

こちらもご覧ください

適用対象