共用方式為


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 Unicode 字元超出 ASCII 範圍時,會造成資料遺失。

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 僅支援有限的字元集, UTF8EncodingUnicodeEncodingUTF32Encoding 類別更適合全球化的應用。 以下考量可協助您決定是否使用 ASCIIEncoding

  • 有些協定需要 ASCII 或其子集。 在這些情況下,ASCII 編碼是合適的。

  • 如果預期會用 8 位元編碼,那 ASCII 可能不是正確的選擇。 相反地,建議考慮使用 UTF8 代替 ASCII。 對於 U+0000 到 U+007F 這些字元,結果相同,但所有 Unicode 字元都可用 UTF-8 表示,避免資料遺失。

謹慎

ASCIIEncoding 無法提供錯誤偵測。 出於安全考量,你應該使用 UTF8EncodingUnicodeEncodingUTF32Encoding ,並啟用錯誤偵測。

GetByteCount 方法決定編碼一組 Unicode 字元的位元組數,並 GetBytes 執行實際編碼。

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

請注意, ASCIIEncoding 預設建構子本身可能無法為你的應用程式提供適當的行為。 你可以考慮將 or DecoderFallback 屬性設EncoderFallbackEncoderExceptionFallbackDecoderExceptionFallback或防止第 8 位元設定的序列。 自訂行為也可能適用於這些情況。

建構函式

名稱 Description
ASCIIEncoding()

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

屬性

名稱 Description
BodyName

當在衍生類別中覆寫時,會獲得一個可用於郵件代理體標籤的當前編碼名稱。

(繼承來源 Encoding)
CodePage

當在衍生類別中被覆寫時,會獲得目前 Encoding的代碼頁識別碼。

(繼承來源 Encoding)
DecoderFallback

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

(繼承來源 Encoding)
EncoderFallback

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

(繼承來源 Encoding)
EncodingName

當在衍生類別中覆寫時,會得到目前編碼的人類可讀描述。

(繼承來源 Encoding)
HeaderName

當 在衍生類別中被覆寫時,會獲得一個可用於郵件代理標頭標籤的當前編碼名稱。

(繼承來源 Encoding)
IsBrowserDisplay

當 在衍生類別中覆寫時,會獲得一個值,表示瀏覽器客戶端是否可以使用目前的編碼來顯示內容。

(繼承來源 Encoding)
IsBrowserSave

當在衍生類別中覆寫時,會獲得一個值,表示瀏覽器客戶端是否能使用目前的編碼來儲存內容。

(繼承來源 Encoding)
IsMailNewsDisplay

當在派生類別中覆寫時,會獲得一個值,表示目前的編碼是否可用於郵件和新聞用戶端顯示內容。

(繼承來源 Encoding)
IsMailNewsSave

當在衍生類別中覆寫時,會獲得一個值,表示目前的編碼是否能被郵件和新聞客戶端用來儲存內容。

(繼承來源 Encoding)
IsReadOnly

當在衍生類別中覆寫時,會獲得一個值,表示目前編碼是否為唯讀。

(繼承來源 Encoding)
IsSingleByte

會取得一個值,表示目前編碼是否使用單位元組碼點。

Preamble

當在衍生類別中覆寫時,會回傳一個包含指定所用編碼序列的位元組的區間。

(繼承來源 Encoding)
WebName

當在衍生類別中被覆寫時,會獲得目前編碼的名稱,註冊於網際網路號碼分配管理局(IANA)。

(繼承來源 Encoding)
WindowsCodePage

當在衍生類別中覆寫時,會得到與目前編碼最接近的 Windows 作業系統代碼頁。

(繼承來源 Encoding)

方法

名稱 Description
Clone()

當在衍生類別中覆寫時,會產生目前 Encoding 物件的淺層副本。

(繼承來源 Encoding)
Equals(Object)

判斷指定的 Object 是否等於當前實例。

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

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

GetByteCount(Char[])

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

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

計算從指定字元指標開始編碼一組字元所產生的位元組數。

GetByteCount(ReadOnlySpan<Char>)

計算編碼指定字元區間所產生的位元組數。

GetByteCount(ReadOnlySpan<Char>)

當在派生類別中覆寫時,會計算在指定字元區間內編碼字元所產生的位元組數。

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

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

(繼承來源 Encoding)
GetByteCount(String)

計算透過編碼指定 String字元所產生的位元組數。

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

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

GetBytes(Char[], Int32, Int32)

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

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

當在派生類別中覆寫時,會將指定字元陣列中的所有字元編碼成一串位元組。

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

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

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

將指定的字元區間編碼為指定的位元組區間。

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

當在派生類別中覆寫時,會將指定唯讀區間的一組字元編碼到位元組區間。

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

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

GetBytes(String, Int32, Int32)

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

(繼承來源 Encoding)
GetBytes(String)

當在派生類別中覆寫時,會將指定字串中的所有字元編碼成一串位元組。

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

計算從指定位元組陣列解碼一串位元組所產生的字元數。

GetCharCount(Byte[])

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

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

透過解碼從指定位元組指標開始的一串位元組來計算字元數。

GetCharCount(ReadOnlySpan<Byte>)

透過解碼指定的位元組範圍來計算字元數。

GetCharCount(ReadOnlySpan<Byte>)

當在衍生類別中覆寫時,會計算出解碼所提供的唯讀位元組區間所產生的字元數。

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

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

GetChars(Byte[], Int32, Int32)

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

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

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

(繼承來源 Encoding)
GetChars(Byte*, 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, Int32)

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

GetString(Byte[])

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

GetString(Byte[])

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

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

當在派生類別中覆寫時,會將從指定位址開始的指定位元組解碼成字串。

(繼承來源 Encoding)
GetString(ReadOnlySpan<Byte>)

當在派生類別中覆寫時,會將指定位元組區間中的所有位元組解碼成一個字串。

(繼承來源 Encoding)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsAlwaysNormalized()

會得到一個值,表示目前編碼是否總是正規化,使用預設的正規化形式。

(繼承來源 Encoding)
IsAlwaysNormalized(NormalizationForm)

當在派生類別中覆寫時,會得到一個值,表示目前編碼是否總是被正規化,並使用指定的正規化形式。

(繼承來源 Encoding)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

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

如果目的地夠大,則會將指定唯讀區段中的一組字元編碼成位元組區間。

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

如果目的地夠大,則會將指定唯讀區段中的一組位元組解碼成字元區間。

擴充方法

名稱 Description
GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>)

利用指定Encoding條件解碼 ReadOnlySequence<T>bytes,並將結果寫入 writer

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

將指定 ReadOnlySequence<T> 內容編碼為 bytes,並 Encoding 輸出結果為 bytes

GetBytes(Encoding, ReadOnlySequence<Char>)

將指定 ReadOnlySequence<T> 編碼成 Byte 一個陣列,使用指定的 Encoding

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

將指定 ReadOnlySpan<T> 內容編碼為 bytes,使用指定 Encoding 條件,並將結果寫入 writer

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

利用指定Encoding條件解碼 ReadOnlySequence<T>chars,並將結果寫入 writer

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

利用指定Encoding條件將指定 ReadOnlySequence<T>chars 解碼至 s,並將結果輸出為 chars

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

利用指定Encoding條件解碼 ReadOnlySpan<T>chars,並將結果寫入 writer

GetString(Encoding, ReadOnlySequence<Byte>)

利用指定的 Encoding將 解碼ReadOnlySequence<T>為 。String

適用於

另請參閱