ASCIIEncoding.GetChars 方法

定義

將位元組序列解碼成一組字元。

多載

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

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

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

將起始於指定位元組指標的位元組序列解碼成一組字元;儲存該組字元時,係以指定的字元指標為起始點。

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

將指定位元組陣列中的位元組序列解碼成指定的字元陣列。

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

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

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>

包含所產生之字元集合的範圍。

傳回

Int32

寫入 chars 的實際字元數。

備註

若要計算儲存所產生字元所需的 GetChars 確切大小,請使用 GetCharCount 。 若要計算大小上限,請使用 GetMaxCharCount 。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

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

ASCIIEncoding 不提供錯誤偵測。 任何大於十六進位 0x7F 的位元組會解碼為 Unicode 問號 (「?」) 。

警告

基於安全性考慮,您應該使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

適用於

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

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Text.ASCIIEncoding.GetChars(Byte[], Int32, Int32, Char[], Int32)

將起始於指定位元組指標的位元組序列解碼成一組字元;儲存該組字元時,係以指定的字元指標為起始點。

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

要寫入的最大字元數。

傳回

Int32

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

屬性

例外狀況

bytesnull

-或- charsnull

byteCountcharCount 小於零。

charCount 小於結果字元數。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback

備註

若要計算儲存所產生字元所需的 GetChars 確切陣列大小,應用程式會使用 GetCharCount 。 若要計算陣列大小上限,應用程式應該使用 GetMaxCharCount 。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

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

ASCIIEncoding 不提供錯誤偵測。 任何大於十六進位0x7F的位元組會解碼為 Unicode 問號 (「?」) 。

警告

基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

另請參閱

適用於

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

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

傳回

Int32

寫入 chars 的實際字元數。

例外狀況

bytesnull

-或- charsnull

byteIndexbyteCountcharIndex 小於零。

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

-或- charIndexchars 中不是有效的索引。

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

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例示範如何解碼位元組陣列中的元素範圍,並將結果儲存在 Unicode 字元陣列中的一組元素中。

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<Byte>^bytes = {65,83,67,73,73,32,69,110,99,111,100,105,110,103,32,69,120,97,109,112,108,101};
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   int charCount = ascii->GetCharCount( bytes, 6, 8 );
   chars = gcnew array<Char>(charCount);
   int charsDecodedCount = ascii->GetChars( bytes, 6, 8, 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 ASCIIEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             65,  83,  67,  73,  73,  32,  69,
            110,  99, 111, 100, 105, 110, 103,
             32,  69, 120,  97, 109, 112, 108, 101
        };

        ASCIIEncoding ascii = new ASCIIEncoding();

        int charCount = ascii.GetCharCount(bytes, 6, 8);
        chars = new Char[charCount];
        int charsDecodedCount = ascii.GetChars(bytes, 6, 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 ASCIIEncodingExample
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
             65,  83,  67,  73,  73,  32,  69, _
            110,  99, 111, 100, 105, 110, 103, _
             32,  69, 120,  97, 109, 112, 108, 101}

        Dim ascii As New ASCIIEncoding()

        Dim charCount As Integer = ascii.GetCharCount(bytes, 6, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = ascii.GetChars(bytes, 6, 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 方法通常會執行得更快。

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

ASCIIEncoding 不提供錯誤偵測。 任何大於十六進位0x7F的位元組會解碼為 Unicode 問號 (「?」) 。

警告

基於安全性考慮,建議您使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

另請參閱

適用於