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

將指定的位元組範圍解碼為指定的字元範圍。

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 方法或 方法所提供的 GetDecoderEncoderGetEncoder 物件。

適用於

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

來源:
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 所表示的位置上寫入的實際字元數。

屬性

例外狀況

bytesnull

-或-

charsnull

byteCountcharCount 小於零。

已啟用錯誤偵測,而且 bytes 包含無效的位元組序列。

-或-

charCount 小於結果字元數。

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

備註

若要計算儲存所產生字元所需的 GetChars 確切陣列大小,請呼叫 GetCharCount 方法。 若要計算陣列大小上限,請呼叫 GetMaxCharCount 方法。 方法 GetCharCount 通常會配置較少的記憶體,而 GetMaxCharCount 方法通常執行速度較快。

在錯誤偵測中,不正確序列會導致此方法擲回 ArgumentException 例外狀況。 如果沒有錯誤偵測,則會忽略不正確序列,而且不會擲回任何例外狀況。

如果要解碼的位元組範圍包含位元組順序標記 (BOM) ,而且位元組陣列是由非 BOM 感知類型的方法傳回,則這個字元 U+FFFE 會包含在此方法傳回的字元陣列中。 您可以呼叫 String.TrimStart 方法來移除它。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,請分別使用 Decoder 方法或 方法所提供的 GetDecoderEncoderGetEncoder 物件。

另請參閱

適用於

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

來源:
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 的實際字元數。

例外狀況

bytesnull

-或-

charsnull

byteIndexbyteCountcharIndex 小於零。

-或-

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

-或-

charIndexchars 中不是有效的索引。

已啟用錯誤偵測,而且 bytes 包含無效的位元組序列。

-或-

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

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

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例會 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 方法來移除它。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分割成較小的區塊,請分別使用 DecoderEncoder 方法或 GetEncoder 方法所提供的 GetDecoder

另請參閱

適用於