閱讀英文

共用方式為


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)

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

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

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

傳回

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

備註

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

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

如果 GetChars 呼叫 時 flush 設定為 false,譯碼器會將尾端位元組儲存在內部緩衝區中的數據區塊結尾,並在下一個譯碼作業中使用它們。 應用程式應該在呼叫相同區塊之前,立即在數據區塊上呼叫 GetCharCountGetChars ,以便計算中包含上一個區塊的任何尾端位元組。

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

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

重要

此 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);
[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
[<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

傳回

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

屬性

例外狀況

bytesnull (Nothing)。

-或-

charsnull (Nothing)。

byteCountcharCount 小於零。

charCount 小於結果字元數。

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

-和-

Fallback 設定為 DecoderExceptionFallback

備註

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

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

如果 GetChars 呼叫 時 flush 設定為 false,譯碼器會將尾端位元組儲存在內部緩衝區中的數據區塊結尾,並在下一個譯碼作業中使用它們。 應用程式應該在呼叫相同區塊之前,立即在數據區塊上呼叫 GetCharCountGetChars ,以便計算中包含上一個區塊的任何尾端位元組。

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

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

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

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

傳回

寫入 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之間儲存狀態。 當應用程式使用數據流完成時,它應該將 參數設定 flushtrue ,以確保已排清狀態資訊。 使用此設定,譯碼器會忽略數據區塊結尾的無效位元組,並清除內部緩衝區。

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

如果 GetChars 呼叫 時 flush 設定為 false,譯碼器會將尾端位元組儲存在內部緩衝區中的數據區塊結尾,並在下一個譯碼作業中使用它們。 應用程式應該在呼叫相同區塊之前,立即在數據區塊上呼叫 GetCharCountGetChars ,以便計算中包含上一個區塊的任何尾端位元組。

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
Decoder.cs
來源:
Decoder.cs
來源:
Decoder.cs

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

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

傳回

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

例外狀況

bytesnull (Nothing)。

-或-

charsnull (Nothing)。

byteIndexbyteCountcharIndex 小於零。

-或-

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

-或-

charIndexchars 中不是有效的索引。

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

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

-和-

Fallback 設定為 DecoderExceptionFallback

備註

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

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

如果 GetChars 呼叫 時 flush 設定為 false,譯碼器會將尾端位元組儲存在內部緩衝區中的數據區塊結尾,並在下一個譯碼作業中使用它們。 應用程式應該在呼叫相同區塊之前,立即在數據區塊上呼叫 GetCharCountGetChars ,以便計算中包含上一個區塊的任何尾端位元組。

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

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0