UnicodeEncoding.GetChars 方法

定義

將一串位元組解碼成一組字元。

多載

名稱 Description
GetChars(Byte*, Int32, Char*, Int32)

將從指定位元組指標開始的一串位元組解碼成一組字元,這些字元從指定字元指標開始儲存。

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

將指定位元組陣列的一串位元組解碼到指定的字元陣列。

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

重要

此 API 不符合 CLS 規範。

將從指定位元組指標開始的一串位元組解碼成一組字元,這些字元從指定字元指標開始儲存。

public:
 override int GetChars(System::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.Runtime.InteropServices.ComVisible(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)>]
override this.GetChars : nativeptr<byte> * int * nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(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

參數

bytes
Byte*

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

byteCount
Int32

需要解碼的位元組數。

chars
Char*

指向開始寫入所得字元集合的位置指標。

charCount
Int32

要寫入的字元數上限。

傳回

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

屬性

例外狀況

bytesnullNothing)。

-或-

charsnullNothing)。

byteCountcharCount 小於零。

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

-或-

charCount 小於所得字元數。

後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼

-及-

DecoderFallback 設定為 DecoderExceptionFallback

備註

為了計算儲存所得字元所需的精確陣列大小 GetChars ,應用程式使用 GetCharCount。 要計算最大陣列大小,應用程式應使用 GetMaxCharCount。 此 GetCharCount 方法通常分配較少記憶體,而 GetMaxCharCount 執行速度較快。

在錯誤檢測中,若序列無效,該方法會拋出 ArgumentException。 若無錯誤偵測,無效序列會被忽略,且不會拋出例外。

若解碼的位元組範圍包含位元組順序標記(BOM),且位元組陣列是由非 BOM 識別型態的方法回傳,則字元 U+FFFE 會包含在該方法回傳的字元陣列中。 你可以透過呼叫 String.TrimStart 該方法來移除它。

待轉換的資料,例如從串流讀取的資料,可能只能以連續區塊形式取得。 在這種情況下,或資料量過大需要分割成較小區塊時,應用程式應分別使用 DecoderEncoder 或 方法所提供的GetDecoderGetEncoder物件。

另請參閱

適用於

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

將指定位元組陣列的一串位元組解碼到指定的字元陣列。

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的字元數。

例外狀況

bytesnullNothing)。

-或-

charsnullNothing)。

byteIndexbyteCountcharIndex 小於零。

-或-

byteIndexbyteCount 不表示在 中的 bytes有效範圍。

-或-

charIndex 在 中 不是有效的指標 chars

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

-或-

chars 從陣列到末端的容量 charIndex 不足以容納產生的字元。

後來出現了備用機制(更多資訊請參見 .NET 中的字元編碼

-及-

DecoderFallback 設定為 DecoderExceptionFallback

範例

以下範例示範如何使用此 GetChars 方法解碼位元組陣列中的元素範圍,並將結果儲存在字元陣列中。

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

        UnicodeEncoding Unicode = new UnicodeEncoding();

        int charCount = Unicode.GetCharCount(bytes, 2, 8);
        chars = new Char[charCount];
        int charsDecodedCount = Unicode.GetChars(bytes, 2, 8, 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 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 uni As New UnicodeEncoding()
        
        Dim charCount As Integer = uni.GetCharCount(bytes, 2, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = uni.GetChars(bytes, 2, 8, 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

另請參閱

適用於