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 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因此,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 确定对字节序列进行解码的字符数,以及 GetChars 执行 GetString 实际解码的方法。
请注意,默认 ASCIIEncoding 构造函数本身可能没有适合应用程序的行为。 可能需要考虑将或属性EncoderExceptionFallback设置为EncoderFallback或DecoderFallbackDecoderExceptionFallback阻止具有第 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 |
获取一个值,该值指示当前编码是否使用单字节码位。 |
| Preamble |
在派生类中重写时,返回包含指定所用编码的字节序列的范围。 (继承自 Encoding) |
| WebName |
在派生类中重写时,获取为当前编码向 Internet 分配号码颁发机构(IANA)注册的名称。 (继承自 Encoding) |
| WindowsCodePage |
在派生类中重写时,获取与当前编码最接近的 Windows 操作系统代码页。 (继承自 Encoding) |
方法
| 名称 | 说明 |
|---|---|
| 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) |
在派生类中重写时,从指定的 |
| 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) |
如果目标足够大,则从指定的只读范围将字符范围解码为一组字节。 |
扩展方法
| 名称 | 说明 |
|---|---|
| GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>) |
使用指定的ReadOnlySequence<T> |
| GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>) |
使用指定的Encoding值对指定 |
| GetBytes(Encoding, ReadOnlySequence<Char>) |
使用指定的Encoding数组将指定的ReadOnlySequence<T>值编码为Byte数组。 |
| GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>) |
使用指定的Encoding值对指定 |
| GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>) |
使用指定的ReadOnlySequence<T> |
| GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>) |
使用指定的ReadOnlySequence<T> |
| GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>) |
使用指定的ReadOnlySpan<T> |
| GetString(Encoding, ReadOnlySequence<Byte>) |
使用指定的值将指定的 ReadOnlySequence<T> 值解码为 String 指定的 Encoding。 |