ASCIIEncoding 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 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
		- 继承
 
- 属性
 
示例
以下示例演示如何将 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 字符限制为从 U+0000 到 U+007F 的最低 128 个 Unicode 字符。 如果使用 属性 Encoding.ASCII 或 ASCIIEncoding 构造函数返回的默认编码器,在执行编码操作之前,将用问号 (?) 替换该范围之外的字符。 ASCIIEncoding由于 类仅支持有限的字符集,UTF8Encoding因此 、 UnicodeEncoding和 UTF32Encoding 类更适合全球化应用程序。 以下注意事项可帮助你决定是否使用 ASCIIEncoding:
某些协议需要 ASCII 或 ASCII 的子集。 在这些情况下,ASCII 编码是合适的。
如果预期使用 8 位编码,则 ASCII 可能不是正确的选择。 相反,请考虑使用 UTF8 而不是 ASCII。 对于字符 U+0000 到 U+007F,结果相同,但所有 Unicode 字符都可以在 UTF-8 中表示,从而避免数据丢失。
注意
ASCIIEncoding 不提供错误检测。 出于安全原因,应使用 UTF8Encoding、 UnicodeEncoding或 UTF32Encoding 并启用错误检测。
GetByteCount方法确定导致对一组 Unicode 字符进行编码的字节数, GetBytes 方法执行实际编码。
同样, GetCharCount 方法确定导致解码字节序列的字符数,和 GetCharsGetString 方法执行实际解码。
请注意,默认 ASCIIEncoding 构造函数本身可能没有适合应用程序的行为。 建议考虑将 或 DecoderFallback 属性设置为 EncoderFallbackEncoderExceptionFallback 或 DecoderExceptionFallback ,以防止设置了第 8 位的序列。 自定义行为可能也适用于这些情况。
构造函数
| ASCIIEncoding() | 
		 初始化 ASCIIEncoding 类的新实例。  | 
        	
属性
| 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 | 
		 获取一个可以指示当前编码是否使用单字节码位的值。  | 
        	
| IsSingleByte | 
		 在派生类中重写时,获取一个值,该值指示当前的编码是否使用单字节码位。 (继承自 Encoding) | 
        	
| Preamble | 
		 在派生类中重写时,返回包含指定所用编码的字节序列的范围。 (继承自 Encoding) | 
        	
| WebName | 
		 在派生类中重写时,获取在 Internet 编号分配管理机构 (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) | 
		 在派生类中重写时,从指定的   | 
        	
| 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> 解码为   | 
        	
| GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 编码为   | 
        	
| GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySpan<T> 编码为   | 
        	
| GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码为   | 
        	
| GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码为   | 
        	
| GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySpan<T> 解码为   | 
        	
| GetString(Encoding, ReadOnlySequence<Byte>) | 
		 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码到 String。  |