ASCIIEncoding 類別

定義

代表 Unicode 字元的 ASCII 字元編碼方式。

public ref class ASCIIEncoding : System::Text::Encoding
public class ASCIIEncoding : System.Text.Encoding
[System.Serializable]
public class ASCIIEncoding : System.Text.Encoding
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ASCIIEncoding : System.Text.Encoding
type ASCIIEncoding = class
    inherit Encoding
[<System.Serializable>]
type ASCIIEncoding = class
    inherit Encoding
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ASCIIEncoding = class
    inherit Encoding
Public Class ASCIIEncoding
Inherits Encoding
繼承
ASCIIEncoding
屬性

範例

下列範例示範如何將 Unicode 字元編碼為 ASCII。 請注意,當應用程式用來 ASCIIEncoding 編碼 ASCII 範圍以外的 Unicode 字元時,所發生的資料遺失。

using namespace System;
using namespace System::Collections;
using namespace System::Text;
int main()
{
   
   // The encoding.
   ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
   
   // A Unicode string with two characters outside the ASCII code range.
   String^ unicodeString = L"This Unicode String* contains two characters with codes outside the ASCII code range, Pi (\u03a0) and Sigma (\u03a3).";
   Console::WriteLine( "Original String*:" );
   Console::WriteLine( unicodeString );
   
   // Save positions of the special characters for later reference.
   int indexOfPi = unicodeString->IndexOf( L'\u03a0' );
   int indexOfSigma = unicodeString->IndexOf( L'\u03a3' );
   
   // Encode string.
   array<Byte>^encodedBytes = ascii->GetBytes( unicodeString );
   Console::WriteLine();
   Console::WriteLine( "Encoded bytes:" );
   IEnumerator^ myEnum = encodedBytes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Byte b = safe_cast<Byte>(myEnum->Current);
      Console::Write( "->Item[ {0}]", b );
   }

   Console::WriteLine();
   
   // Notice that the special characters have been replaced with
   // the value 63, which is the ASCII character code for '?'.
   Console::WriteLine();
   Console::WriteLine( "Value at position of Pi character: {0}", encodedBytes[ indexOfPi ] );
   Console::WriteLine( "Value at position of Sigma character: {0}", encodedBytes[ indexOfSigma ] );
   
   // Decode bytes back to string.
   // Notice missing Pi and Sigma characters.
   String^ decodedString = ascii->GetString( encodedBytes );
   Console::WriteLine();
   Console::WriteLine( "Decoded bytes:" );
   Console::WriteLine( decodedString );
}
// The example displays the following output:
//    Original string:
//    This Unicode string contains two characters with codes outside the ASCII code ra
//    nge, Pi (Π) and Sigma (Σ).
//
//    Encoded bytes:
//    [84][104][105][115][32][85][110][105][99][111][100][101][32][115][116][114][105]
//    [110][103][32][99][111][110][116][97][105][110][115][32][116][119][111][32][99][
//    104][97][114][97][99][116][101][114][115][32][119][105][116][104][32][99][111][1
//    00][101][115][32][111][117][116][115][105][100][101][32][116][104][101][32][65][
//    83][67][73][73][32][99][111][100][101][32][114][97][110][103][101][44][32][80][1
//    05][32][40][63][41][32][97][110][100][32][83][105][103][109][97][32][40][63][41]
//    [46]
//
//    Value at position of Pi character: 63
//    Value at position of Sigma character: 63
//
//    Decoded bytes:
//    This Unicode string contains two characters with codes outside the ASCII code ra
//    nge, Pi (?) and Sigma (?).
using System;
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        // The encoding.
        ASCIIEncoding ascii = new ASCIIEncoding();
        
        // A Unicode string with two characters outside the ASCII code range.
        String unicodeString =
            "This Unicode string contains two characters " +
            "with codes outside the ASCII code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Save positions of the special characters for later reference.
        int indexOfPi = unicodeString.IndexOf('\u03a0');
        int indexOfSigma = unicodeString.IndexOf('\u03a3');

        // Encode string.
        Byte[] encodedBytes = ascii.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
        
        // Notice that the special characters have been replaced with
        // the value 63, which is the ASCII character code for '?'.
        Console.WriteLine();
        Console.WriteLine(
            "Value at position of Pi character: {0}",
            encodedBytes[indexOfPi]
        );
        Console.WriteLine(
            "Value at position of Sigma character: {0}",
            encodedBytes[indexOfSigma]
        );

        // Decode bytes back to string.
        // Notice missing Pi and Sigma characters.
        String decodedString = ascii.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}
// The example displays the following output:
//    Original string:
//    This Unicode string contains two characters with codes outside the ASCII code ra
//    nge, Pi (Π) and Sigma (Σ).
//
//    Encoded bytes:
//    [84][104][105][115][32][85][110][105][99][111][100][101][32][115][116][114][105]
//    [110][103][32][99][111][110][116][97][105][110][115][32][116][119][111][32][99][
//    104][97][114][97][99][116][101][114][115][32][119][105][116][104][32][99][111][1
//    00][101][115][32][111][117][116][115][105][100][101][32][116][104][101][32][65][
//    83][67][73][73][32][99][111][100][101][32][114][97][110][103][101][44][32][80][1
//    05][32][40][63][41][32][97][110][100][32][83][105][103][109][97][32][40][63][41]
//    [46]
//
//    Value at position of Pi character: 63
//    Value at position of Sigma character: 63
//
//    Decoded bytes:
//    This Unicode string contains two characters with codes outside the ASCII code ra
//    nge, Pi (?) and Sigma (?).
Imports System.Text

Class ASCIIEncodingExample
    Public Shared Sub Main()
        ' The encoding.
        Dim ascii As New ASCIIEncoding()

        ' A Unicode string with two characters outside the ASCII code range.
        Dim unicodeString As String = _
            "This Unicode string contains two characters " & _
            "with codes outside the ASCII code range, " & _
            "Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")."
        Console.WriteLine("Original string:")
        Console.WriteLine(unicodeString)

        ' Save positions of the special characters for later reference.
        Dim indexOfPi As Integer = unicodeString.IndexOf(ChrW(928))
        Dim indexOfSigma As Integer = unicodeString.IndexOf(ChrW(931))

        ' Encode string.
        Dim encodedBytes As Byte() = ascii.GetBytes(unicodeString)
        Console.WriteLine()
        Console.WriteLine("Encoded bytes:")
        Dim b As Byte
        For Each b In encodedBytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()

        ' Notice that the special characters have been replaced with
        ' the value 63, which is the ASCII character code for '?'.
        Console.WriteLine()
        Console.WriteLine( _
            "Value at position of Pi character: {0}", _
            encodedBytes(indexOfPi) _
        )
        Console.WriteLine( _
            "Value at position of Sigma character: {0}", _
            encodedBytes(indexOfSigma) _
        )

        ' Decode bytes back to string.
        ' Notice missing Pi and Sigma characters.
        Dim decodedString As String = ascii.GetString(encodedBytes)
        Console.WriteLine()
        Console.WriteLine("Decoded bytes:")
        Console.WriteLine(decodedString)
    End Sub
End Class
' The example displays the following output:
'    Original string:
'    This Unicode string contains two characters with codes outside the ASCII code ra
'    nge, Pi (Π) and Sigma (Σ).
'
'    Encoded bytes:
'    [84][104][105][115][32][85][110][105][99][111][100][101][32][115][116][114][105]
'    [110][103][32][99][111][110][116][97][105][110][115][32][116][119][111][32][99][
'    104][97][114][97][99][116][101][114][115][32][119][105][116][104][32][99][111][1
'    00][101][115][32][111][117][116][115][105][100][101][32][116][104][101][32][65][
'    83][67][73][73][32][99][111][100][101][32][114][97][110][103][101][44][32][80][1
'    05][32][40][63][41][32][97][110][100][32][83][105][103][109][97][32][40][63][41]
'    [46]
'
'    Value at position of Pi character: 63
'    Value at position of Sigma character: 63
'
'    Decoded bytes:
'    This Unicode string contains two characters with codes outside the ASCII code ra
'    nge, Pi (?) and Sigma (?).

備註

編碼是將一組 Unicode 字元轉換成位元組序列的處理程序。 解碼是將編碼位元組序列轉換成一組 Unicode 字元的程式。

ASCIIEncoding 對應至 Windows 字碼頁 20127。 因為 ASCII 是 7 位編碼,所以 ASCII 字元限制為最低 128 Unicode 字元,從 U+0000 到 U+007F。 如果您使用 屬性或 ASCIIEncoding 建構函式傳 Encoding.ASCII 回的預設編碼器,該範圍以外的字元會取代為問號 (?) ,再執行編碼作業。 因為 類別 ASCIIEncoding 只支援有限的字元集, UTF8Encoding 所以 、 UnicodeEncodingUTF32Encoding 類別更適合用於全球化應用程式。 下列考慮可協助您決定是否要使用 ASCIIEncoding

  • 某些通訊協定需要 ASCII 或 ASCII 子集。 在這些情況下,ASCII 編碼是適當的。

  • 如果預期有 8 位編碼,則 ASCII 可能不是正確的選擇。 相反地,請考慮使用 UTF8 而非 ASCII。 對於 U+0000 到 U+007F 的字元,結果完全相同,但所有 Unicode 字元都可在 UTF-8 中表示,以避免資料遺失。

注意

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

方法 GetByteCount 會決定一組 Unicode 字元編碼所產生的位元組數目,而 GetBytes 方法會執行實際的編碼。

同樣地, GetCharCount 方法會決定解碼一連串位元組的字元數,而 GetCharsGetString 方法會執行實際的解碼。

請注意,預設 ASCIIEncoding 建構函式本身可能沒有適合您的應用程式行為。 您可能想要考慮將 或 DecoderFallback 屬性設定 EncoderFallbackEncoderExceptionFallbackDecoderExceptionFallback ,以防止第 8 位設定的序列。 自訂行為可能也適用于這些情況。

建構函式

ASCIIEncoding()

初始化 ASCIIEncoding 類別的新執行個體。

屬性

BodyName

在衍生類別中覆寫時,取得可以與郵件代理程式主體標籤一起使用的目前編碼方式名稱。

(繼承來源 Encoding)
CodePage

在衍生類別中覆寫時,取得目前 Encoding 的字碼頁識別項。

(繼承來源 Encoding)
DecoderFallback

取得或設定目前 DecoderFallback 物件的 Encoding 物件。

(繼承來源 Encoding)
EncoderFallback

取得或設定目前 EncoderFallback 物件的 Encoding 物件。

(繼承來源 Encoding)
EncodingName

在衍生類別中覆寫時,取得目前編碼方式的人們可讀取 (Human-Readable) 的描述。

(繼承來源 Encoding)
HeaderName

在衍生類別中覆寫時,取得可以與郵件代理程式標頭標籤一起使用的目前編碼方式名稱。

(繼承來源 Encoding)
IsBrowserDisplay

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來顯示內容。

(繼承來源 Encoding)
IsBrowserSave

在衍生類別中覆寫時,取得值,指出瀏覽器用戶端是否可以使用目前的編碼方式來儲存內容。

(繼承來源 Encoding)
IsMailNewsDisplay

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來顯示內容。

(繼承來源 Encoding)
IsMailNewsSave

在衍生類別中覆寫時,取得值,指出郵件和新聞用戶端是否可以使用目前的編碼方式來儲存內容。

(繼承來源 Encoding)
IsReadOnly

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否為唯讀。

(繼承來源 Encoding)
IsSingleByte

取得值,指出目前的編碼方式是否使用單一位元組字碼指標。

IsSingleByte

在衍生類別中覆寫時,取得值,指出目前的編碼方式是否使用單一位元組字碼指標。

(繼承來源 Encoding)
Preamble

在衍生類別中覆寫時,傳回範圍,其包含指定所用編碼方式的位元組序列。

(繼承來源 Encoding)
WebName

在衍生類別中覆寫時,若要取得目前的編碼方式,請取得向 Internet Assigned Numbers Authority (IANA) 註冊的名稱。

(繼承來源 Encoding)
WindowsCodePage

在衍生類別中覆寫時,請取得最能符合目前編碼方式的 Windows 作業系統字碼頁。

(繼承來源 Encoding)

方法

Clone()

在衍生類別中覆寫時,會建立目前 Encoding 物件的淺層複本。

(繼承來源 Encoding)
Equals(Object)

判斷指定的 Object 和目前的執行個體是否相等。

(繼承來源 Encoding)
GetByteCount(Char*, Int32)

計算將起始於指定字元指標的一組字元編碼所產生的位元組數目。

GetByteCount(Char*, Int32)

在衍生類別中覆寫時,計算從指定的字元指標開始,編碼一組字元所產生的位元組數目。

(繼承來源 Encoding)
GetByteCount(Char[])

在衍生類別中覆寫時,計算編碼指定字元陣列中所有字元所產生的位元組數目。

(繼承來源 Encoding)
GetByteCount(Char[], Int32, Int32)

計算將指定字元陣列中的一組字元編碼所產生的位元組數目。

GetByteCount(ReadOnlySpan<Char>)

計算將指定字元範圍編碼所產生的位元組數目。

GetByteCount(ReadOnlySpan<Char>)

在衍生類別中覆寫時,計算藉由編碼指定字元範圍中字元所產生的位元組數。

(繼承來源 Encoding)
GetByteCount(String)

計算將指定 String 中的字元編碼所產生的位元組數目。

GetByteCount(String, Int32, Int32)

在衍生類別中覆寫時,計算藉由從指定的字串編碼一組字元所產生的位元組數。

(繼承來源 Encoding)
GetBytes(Char*, Int32, Byte*, Int32)

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

GetBytes(Char*, Int32, Byte*, Int32)

在衍生類別中覆寫時,從指定字元指標開始將一組字元編碼成位元組序列 (會從指定的位元組指標開始存放這些位元組)。

(繼承來源 Encoding)
GetBytes(Char[])

在衍生類別中覆寫時,將指定字元陣列中的所有字元編碼成位元組序列。

(繼承來源 Encoding)
GetBytes(Char[], Int32, Int32)

在衍生類別中覆寫時,將指定字元陣列中的一組字元編碼成位元組序列。

(繼承來源 Encoding)
GetBytes(Char[], Int32, Int32, Byte[], Int32)

將指定字元陣列中的一組字元編碼成指定的位元組陣列。

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

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

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

在衍生類別中覆寫時,從指定的唯讀範圍將一組字元編碼到位元組範圍。

(繼承來源 Encoding)
GetBytes(String)

在衍生類別中覆寫時,將指定字串中的所有字元編碼成位元組序列。

(繼承來源 Encoding)
GetBytes(String, Int32, Int32)

在衍生類別中覆寫時,從指定字串中指定的 index 開始,將 count 指定的字元數編碼到位元組陣列。

(繼承來源 Encoding)
GetBytes(String, Int32, Int32, Byte[], Int32)

將指定 String 中的一組字元編碼成指定的位元組陣列。

GetCharCount(Byte*, Int32)

計算將起始於指定位元組指標的位元組序列解碼所產生的字元數。

GetCharCount(Byte*, Int32)

在衍生類別中覆寫時,計算從指定的位元組指標開始,解碼位元組序列所產生的字元數目。

(繼承來源 Encoding)
GetCharCount(Byte[])

在衍生類別中覆寫時,計算解碼指定位元組陣列中所有位元組所產生的字元數目。

(繼承來源 Encoding)
GetCharCount(Byte[], Int32, Int32)

計算將指定位元組陣列中的位元組序列解碼所產生的字元數。

GetCharCount(ReadOnlySpan<Byte>)

計算解碼指定位元組範圍所產生的字元數目。

GetCharCount(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,計算藉由解碼所提供唯讀位元組範圍時產生的字元數。

(繼承來源 Encoding)
GetChars(Byte*, Int32, Char*, Int32)

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

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

在衍生類別中覆寫時,從指定位元組指標開始將位元組序列解碼成一組字元 (會從指定的字元指標開始存放這些字元)。

(繼承來源 Encoding)
GetChars(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成一組字元。

(繼承來源 Encoding)
GetChars(Byte[], Int32, Int32)

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

(繼承來源 Encoding)
GetChars(Byte[], Int32, Int32, Char[], Int32)

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

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

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

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

在衍生類別中覆寫時,將指定唯讀位元組範圍中的所有位元組解碼成字元範圍。

(繼承來源 Encoding)
GetDecoder()

取得可以將以 ASCII 編碼的位元組序列轉換成 Unicode 字元序列的解碼器。

GetDecoder()

在衍生類別中覆寫時,取得會將編碼的位元組序列轉換成字元序列的解碼器。

(繼承來源 Encoding)
GetEncoder()

取得可以將 Unicode 字元序列轉換成以 ASCII 編碼的位元組序列的編碼器。

GetEncoder()

在衍生類別中覆寫時,取得會將 Unicode 字元序列轉換成編碼的位元組序列的編碼器。

(繼承來源 Encoding)
GetHashCode()

傳回目前執行個體的雜湊碼。

(繼承來源 Encoding)
GetMaxByteCount(Int32)

計算將指定數目的字元編碼所產生的最大位元組數目。

GetMaxCharCount(Int32)

計算將指定數目的位元組解碼所產生的最大字元數。

GetPreamble()

在衍生類別中覆寫時,傳回可指定所用編碼方式的位元組序列。

(繼承來源 Encoding)
GetString(Byte*, Int32)

在衍生類別中覆寫時,將指定位址開頭之指定數目的位元組解碼為字串。

(繼承來源 Encoding)
GetString(Byte[])

代表 Unicode 字元的 ASCII 字元編碼方式。

GetString(Byte[])

在衍生類別中覆寫時,將指定位元組陣列中的所有位元組解碼成字串。

(繼承來源 Encoding)
GetString(Byte[], Int32, Int32)

將位元組陣列中的某一段位元組範圍解碼成字串。

GetString(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將指定位元組範圍中的所有位元組解碼成字串。

(繼承來源 Encoding)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsAlwaysNormalized()

取得值,指出目前的編碼方式是否永遠都是使用預設的正規化表單進行正規化。

(繼承來源 Encoding)
IsAlwaysNormalized(NormalizationForm)

在衍生類別中覆寫時取得值,指出目前的編碼方式是否永遠都是使用指定的正規化表單進行正規化。

(繼承來源 Encoding)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TryGetBytes(ReadOnlySpan<Char>, Span<Byte>, Int32)

如果目的地夠大,則從指定的唯讀範圍編碼為一組字元的位元組範圍。

TryGetBytes(ReadOnlySpan<Char>, Span<Byte>, Int32)

如果目的地夠大,則從指定的唯讀範圍編碼為一組字元的位元組範圍。

(繼承來源 Encoding)
TryGetChars(ReadOnlySpan<Byte>, Span<Char>, Int32)

如果目的地夠大,就會從指定的唯讀範圍將字元解碼成一組位元組。

TryGetChars(ReadOnlySpan<Byte>, Span<Char>, Int32)

如果目的地夠大,就會從指定的唯讀範圍將字元解碼成一組位元組。

(繼承來源 Encoding)

擴充方法

GetBytes(Encoding, ReadOnlySequence<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 編碼為 Byte 陣列。

GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 byte,並將結果寫入到 writer

GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 編碼為 byte,並將結果輸出到 bytes

GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySpan<T> 編碼為 byte,並將結果寫入到 writer

GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 char,並將結果寫入到 writer

GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 char,並將結果輸出至 chars

GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>)

使用指定的 Encoding,將指定的 ReadOnlySpan<T> 解碼為 char,並將結果寫入到 writer

GetString(Encoding, ReadOnlySequence<Byte>)

使用指定的 Encoding,將指定的 ReadOnlySequence<T> 解碼為 String

適用於

另請參閱