Decoder.GetChars 方法

定義

在衍生類別中覆寫時,將位元組序列解碼成一組字元。

多載

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

在衍生類別中覆寫時,會將範圍位元組序列和內部緩衝區中的任何位元組解碼成從指定字元指標開始儲存的字元集。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

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

在衍生類別中覆寫時,會將由指定位元組指標開始的位元組序列以及內部緩衝區內的任何位元組,解碼成一組字元 (會從指定的字元指標開始存放這些字元)。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

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

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。

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

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

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

在衍生類別中覆寫時,會將範圍位元組序列和內部緩衝區中的任何位元組解碼成從指定字元指標開始儲存的字元集。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

public:
 virtual int GetChars(ReadOnlySpan<System::Byte> bytes, Span<char> chars, bool flush);
public virtual int GetChars (ReadOnlySpan<byte> bytes, Span<char> chars, bool flush);
abstract member GetChars : ReadOnlySpan<byte> * Span<char> * bool -> int
override this.GetChars : ReadOnlySpan<byte> * Span<char> * bool -> int
Public Overridable Function GetChars (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char), flush As Boolean) As Integer

參數

bytes
ReadOnlySpan<Byte>

要解碼的位元組範圍。

chars
Span<Char>

要寫入所產生字元集的範圍。

flush
Boolean

true 表示要在轉換之後清除解碼器的內部狀態,否則為 false

傳回

Int32

寫入 chars 參數所指出範圍的實際字元數。

備註

請記住,物件會在的 Decoder 呼叫之間儲存狀態 GetChars 。 當應用程式使用資料流程完成時,應該將 flush 參數設為, true 以確定狀態資訊已清除。 使用此設定時,解碼器會忽略資料區塊結尾的無效位元組,並清除內部緩衝區。

若要計算 GetChars 儲存產生的字元所需的完全範圍大小,應用程式應該使用 GetCharCount

如果 GetChars 呼叫時 flush 將設定為 false ,則此解碼器會將尾端位元組儲存在內部緩衝區的資料區塊結尾,並在下一次解碼作業中使用它們。 應用程式應該在 GetCharCount 呼叫相同區塊之前立即呼叫資料區塊 GetChars ,如此一來,前面區塊中的任何尾端位元組都會包含在計算中。

如果您的應用程式要轉換輸入資料流程的許多區段,請考慮使用 Convert 方法。 GetChars 如果輸出範圍不夠大,則會擲回例外狀況,但 Convert 會盡可能填滿最多的空間,並傳回寫入的位元組(如果輸出陣列允許至少兩個字元)。 另請參閱 Encoding.GetChars 主題,以取得更多批註。

適用於

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

重要

此 API 不符合 CLS 規範。

在衍生類別中覆寫時,會將由指定位元組指標開始的位元組序列以及內部緩衝區內的任何位元組,解碼成一組字元 (會從指定的字元指標開始存放這些字元)。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

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

參數

bytes
Byte*

要解碼的第一個位元組指標。

byteCount
Int32

要解碼的位元組數。

chars
Char*

開始寫入產生的一組字元之位置指標。

charCount
Int32

要寫入的最大字元數。

flush
Boolean

true 表示要在轉換之後清除解碼器的內部狀態,否則為 false

傳回

Int32

chars 參數所指示位置上寫入的實際字元數目。

屬性

例外狀況

bytesnull (Nothing)。

-或-

charsnull (Nothing)。

byteCountcharCount 小於零。

charCount 小於結果字元數。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 DecoderExceptionFallback

備註

請記住,物件會在的 Decoder 呼叫之間儲存狀態 GetChars 。 當應用程式使用資料流程完成時,應該將 flush 參數設為, true 以確定狀態資訊已清除。 使用此設定時,解碼器會忽略資料區塊結尾的無效位元組,並清除內部緩衝區。

若要計算 GetChars 儲存產生的字元所需的確切緩衝區大小,應用程式應該使用 GetCharCount

如果 GetChars 呼叫時 flush 將設定為 false ,則此解碼器會將尾端位元組儲存在內部緩衝區的資料區塊結尾,並在下一次解碼作業中使用它們。 應用程式應該在 GetCharCount 呼叫相同區塊之前立即呼叫資料區塊 GetChars ,如此一來,前面區塊中的任何尾端位元組都會包含在計算中。

如果您的應用程式要轉換輸入資料流程的許多區段,請考慮使用 Convert 方法。 GetChars 如果輸出緩衝區不夠大,則會擲回例外狀況,但 Convert 會盡可能填滿最多的空間,並傳回讀取的位元組和寫入的字元數,但前提是輸出陣列允許至少兩個字元。 另請參閱 Encoding.GetChars 主題,以取得更多批註。

另請參閱

適用於

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

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。

public:
 abstract int GetChars(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex);
public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex);
abstract member GetChars : byte[] * int * int * char[] * int -> int
Public MustOverride 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

要開始寫入產生的一組字元之索引。

傳回

Int32

寫入 chars 的實際字元數。

例外狀況

bytesnull (Nothing)。

-或-

charsnull (Nothing)。

byteIndexbyteCountcharIndex 小於零。

-或-

byteindexbyteCount 不代表 bytes 中有效的範圍。

-或-

charIndexchars 中不是有效的索引。

chars 到陣列結尾處,charIndex 沒有足夠的容量容納結果字元。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 DecoderExceptionFallback

範例

下列範例示範如何將位元組陣列中的專案範圍解碼,並將其儲存在 Unicode 字元陣列中。 GetCharCount方法是用來計算將已解碼的元素儲存在陣列中所需的字元數 bytes 。 方法會解碼 GetChars 位元組陣列中的指定專案,並將其儲存在新的字元陣列中。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<Byte>^bytes = {85,0,110,0,105,0,99,0,111,0,100,0,101,0};
   Decoder^ uniDecoder = Encoding::Unicode->GetDecoder();
   int charCount = uniDecoder->GetCharCount( bytes, 0, bytes->Length );
   chars = gcnew array<Char>(charCount);
   int charsDecodedCount = uniDecoder->GetChars( bytes, 0, bytes->Length, 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();
}

/* This code example produces the following output.

7 characters used to decode bytes.
Decoded chars: [U][n][i][c][o][d][e]

*/
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
        };

        Decoder uniDecoder = Encoding.Unicode.GetDecoder();

        int charCount = uniDecoder.GetCharCount(bytes, 0, bytes.Length);
        chars = new Char[charCount];
        int charsDecodedCount = uniDecoder.GetChars(bytes, 0, bytes.Length, 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();
    }
}

/* This code example produces the following output.

7 characters used to decode bytes.
Decoded chars: [U][n][i][c][o][d][e]

*/
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 uniDecoder As Decoder = Encoding.Unicode.GetDecoder()
        
        Dim charCount As Integer = uniDecoder.GetCharCount(bytes, 0, bytes.Length)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = _
            uniDecoder.GetChars(bytes, 0, bytes.Length, 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

'This code example produces the following output.
'
'7 characters used to decode bytes.
'Decoded chars: [U][n][i][c][o][d][e]
'

備註

請記住,物件會在的 Decoder 呼叫之間儲存狀態 GetChars 。 當應用程式使用資料流程完成時,應該將 flush 參數設為, true 以確定狀態資訊已清除。 使用此設定時,解碼器會忽略資料區塊結尾的無效位元組,並清除內部緩衝區。

若要計算 GetChars 儲存產生的字元所需的完全陣列大小,應用程式應該使用 GetCharCount

如果 GetChars 呼叫時 flush 將設定為 false ,則此解碼器會將尾端位元組儲存在內部緩衝區的資料區塊結尾,並在下一次解碼作業中使用它們。 應用程式應該在 GetCharCount 呼叫相同區塊之前立即呼叫資料區塊 GetChars ,如此一來,前面區塊中的任何尾端位元組都會包含在計算中。

如果您的應用程式要轉換輸入資料流程的許多區段,請考慮使用 Convert 方法。 GetChars 如果輸出緩衝區不夠大,則會擲回例外狀況,但 Convert 會盡可能填滿最多的空間,並傳回讀取的位元組和寫入的字元數,但前提是輸出陣列允許至少兩個字元。 另請參閱 Encoding.GetChars 主題,以取得更多批註。

另請參閱

適用於

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

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。 參數會指示,在轉換之後是否要清除解碼器的內部狀態。

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

參數

bytes
Byte[]

包含要解碼之位元組序列的位元組陣列。

byteIndex
Int32

要解碼的第一個位元組索引。

byteCount
Int32

要解碼的位元組數。

chars
Char[]

包含產生的一組字元之字元陣列。

charIndex
Int32

要開始寫入產生的一組字元之索引。

flush
Boolean

true 表示要在轉換之後清除解碼器的內部狀態,否則為 false

傳回

Int32

實際寫入 chars 參數的字元數。

例外狀況

bytesnull (Nothing)。

-或-

charsnull (Nothing)。

byteIndexbyteCountcharIndex 小於零。

-或-

byteindexbyteCount 不代表 bytes 中有效的範圍。

-或-

charIndexchars 中不是有效的索引。

chars 到陣列結尾處,charIndex 沒有足夠的容量容納結果字元。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

Fallback 設定為 DecoderExceptionFallback

備註

請記住,物件會在的 Decoder 呼叫之間儲存狀態 GetChars 。 當應用程式使用資料流程完成時,應該將 flush 參數設為, true 以確定狀態資訊已清除。 使用此設定時,解碼器會忽略資料區塊結尾的無效位元組,並清除內部緩衝區。

若要計算 GetChars 儲存產生的字元所需的完全陣列大小,應用程式應該使用 GetCharCount

如果 GetChars 呼叫時 flush 將設定為 false ,則此解碼器會將尾端位元組儲存在內部緩衝區的資料區塊結尾,並在下一次解碼作業中使用它們。 應用程式應該在 GetCharCount 呼叫相同區塊之前立即呼叫資料區塊 GetChars ,如此一來,前面區塊中的任何尾端位元組都會包含在計算中。

如果您的應用程式要轉換輸入資料流程的許多區段,請考慮使用 Convert 方法。 GetChars 如果輸出緩衝區不夠大,則會擲回例外狀況,但 Convert 會盡可能填滿最多的空間,並傳回讀取的位元組和寫入的字元數,但前提是輸出陣列允許至少兩個字元。 另請參閱 Encoding.GetChars 主題,以取得更多批註。

另請參閱

適用於