UTF8Encoding.GetChars メソッド

定義

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

オーバーロード

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

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

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

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

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

指定したバイト配列に格納されているバイト シーケンスを指定した文字配列にデコードします。

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

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
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 呼び出します。

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

適用対象

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

Source:
UTF8Encoding.cs
Source:
UTF8Encoding.cs
Source:
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 によって示される位置に書き込まれる実際の文字数。

属性

例外

bytesnullです。

または

charsnullです。

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

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

または

charCount が結果の文字数より少なくなっています。

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

および

DecoderFallbackDecoderExceptionFallback に設定されます。

注釈

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

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

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

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

こちらもご覧ください

適用対象

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

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

例外

bytesnullです。

または

charsnullです。

byteIndexbyteCount、または charIndex が 0 未満です。

または

byteindex および byteCountbytesにおいて有効な範囲を表していません。

または

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

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

または

chars には、charIndex から配列の末尾までに十分なサイズがなく、結果の文字を格納できません。

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

および

DecoderFallbackDecoderExceptionFallback に設定されます。

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

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<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 = gcnew UTF8Encoding;
   int charCount = utf8->GetCharCount( bytes, 2, 13 );
   chars = gcnew array<Char>(charCount);
   int charsDecodedCount = utf8->GetChars( bytes, 2, 13, chars, 0 );
   Console::WriteLine( "{0} characters used to decode bytes.", charsDecodedCount );
   Console::Write( "Decoded chars: " );
   IEnumerator^ myEnum = chars->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Char c = safe_cast<Char>(myEnum->Current);
      Console::Write( "[{0}]", c.ToString() );
   }

   Console::WriteLine();
}
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 呼び出します。

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

こちらもご覧ください

適用対象