Language

UTF8Encoding.GetChars メソッド

定義

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

オーバーロード

名前 説明
GetChars(ReadOnlySpan<Byte>, Span<Char>)

指定したバイト スパンを指定した文字スパンにデコードします。

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

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

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

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

GetChars(ReadOnlySpan<Byte>, Span<Char>)

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

指定したバイト スパンを指定した文字スパンにデコードします。

public:
 override int GetChars(ReadOnlySpan<System::Byte> bytes, Span<char> chars);
public override int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars);
override this.GetChars : ReadOnlySpan<byte> * Span<char> -> int
Public Overrides Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char)) As Integer

パラメーター

bytes
ReadOnlySpan<Byte>

デコードするバイトを含むスパン。

chars
Span<Char>

結果の文字セットを格納するスパン。

返品

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

注釈

結果の文字を格納するために GetChars に必要な正確なサイズを計算するには、 GetCharCount メソッドを呼び出します。 最大サイズを計算するには、 GetMaxCharCount メソッドを呼び出します。 一般に、 GetCharCount メソッドは割り当てるメモリが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般に速くなります。

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

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

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

適用対象

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

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

重要

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

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

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

パラメーター

bytes
Byte*

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

byteCount
Int32

デコードするバイト数。

chars
Char*

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

charCount
Int32

書き込む最大文字数。

返品

charsによって示される場所に書き込まれた実際の文字数。

属性

例外

bytes は nullです。

-又は-

chars は nullです。

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

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

-又は-

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

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

および

DecoderFallback は DecoderExceptionFallback に設定されます。

注釈

結果の文字を格納するために GetChars に必要な正確な配列サイズを計算するには、 GetCharCount メソッドを呼び出します。 配列の最大サイズを計算するには、 GetMaxCharCount メソッドを呼び出します。 一般に、 GetCharCount メソッドは割り当てるメモリが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般に速くなります。

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

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

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

こちらもご覧ください

適用対象

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

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

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

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に書き込まれた実際の文字数。

例外

bytes は nullです。

-又は-

chars は nullです。

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

-又は-

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

-又は-

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

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

-又は-

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

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

および

DecoderFallback は DecoderExceptionFallback に設定されます。

例

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

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

注釈

結果の文字を格納するために GetChars に必要な正確な配列サイズを計算するには、 GetCharCount メソッドを呼び出します。 配列の最大サイズを計算するには、 GetMaxCharCount メソッドを呼び出します。 一般に、 GetCharCount メソッドは割り当てるメモリが少なくなりますが、 GetMaxCharCount メソッドの実行速度は一般に速くなります。

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

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

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

こちらもご覧ください

適用対象