Encoding.ASCII 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會獲得 ASCII(7 位元)字元集的編碼。
public:
static property System::Text::Encoding ^ ASCII { System::Text::Encoding ^ get(); };
public static System.Text.Encoding ASCII { get; }
static member ASCII : System.Text.Encoding
Public Shared ReadOnly Property ASCII As Encoding
屬性值
ASCII(7位元)字元集的編碼。
範例
以下範例展示了 ASCII 編碼對超出 ASCII 範圍的字元的影響。
using System;
using System.Text;
class EncodingExample
{
public static void Main()
{
// Create an ASCII encoding.
Encoding ascii = Encoding.ASCII;
// 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 the positions of the special characters for later reference.
int indexOfPi = unicodeString.IndexOf('\u03a0');
int indexOfSigma = unicodeString.IndexOf('\u03a3');
// Encode the 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 a string.
// Notice missing the Pi and Sigma characters.
String decodedString = ascii.GetString(encodedBytes);
Console.WriteLine();
Console.WriteLine("Decoded bytes:");
Console.WriteLine(decodedString);
}
}
/*
This code produces the following output.
Original string:
This unicode string contains two characters with codes outside the ASCII code range, Pi (Π) and Sigma (Σ).
Encoded bytes:
[84][104][105][115][32][117][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][100][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][105][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 range, Pi (?) and Sigma (?).
*/
Imports System.Text
Class EncodingExample
Public Shared Sub Main()
' Create and ASCII encoding.
Dim ascii As Encoding = Encoding.ASCII
' 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(&H03A0) & ") and Sigma (" & ChrW(&H03A3) & ")."
Console.WriteLine("Original string:")
Console.WriteLine(unicodeString)
' Save the positions of the special characters for later reference.
Dim indexOfPi As Integer = unicodeString.IndexOf(ChrW(&H03A0))
Dim indexOfSigma As Integer = unicodeString.IndexOf(ChrW(&H03A3))
' Encode the string.
Dim encodedBytes As Byte() = ascii.GetBytes(unicodeString)
Console.WriteLine()
Console.WriteLine("Encoded bytes:")
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 a 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
'This code produces the following output.
'Original string:
'This unicode string contains two characters with codes outside the ASCII code range, Pi (Π) and Sigma (Σ).
'
'Encoded bytes:
'[84][104][105][115][32][117][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]'[100][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][105][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 range, Pi (?) and Sigma (?).
'
備註
ASCII 字元限制在最低 128 個 Unicode 字元,範圍從 U+0000 到 U+007F。
在選擇應用程式的 ASCII 編碼時,請考慮以下幾點:
ASCII 編碼通常適用於需要 ASCII 的協定。
如果你需要 8 位元編碼(有時被誤稱為「ASCII」),建議使用 UTF-8 編碼而非 ASCII 編碼。 對於字元 0-7F,結果相同,但使用 UTF-8 可避免資料遺失,因為允許表示所有可表示的 Unicode 字元。 請注意,ASCII 編碼有第 8 位元的歧義,可能導致惡意使用,但 UTF-8 編碼則消除第 8 位元的歧義。
在 .NET Framework 2.0 版本之前,.NET Framework 允許透過忽略第 8 位元來偽裝。 從 .NET Framework 2.0 開始,非 ASCII 的程式碼點在解碼時會回退。
ASCIIEncoding這個屬性回傳的物件可能沒有適合你的應用程式的行為。 它使用替換備援來替換每個無法編碼的字串,以及每個無法解碼的位元組,並用問號(“?”)字元替換。 相反地,你可以呼叫 GetEncoding(String, EncoderFallback, DecoderFallback) 方法實例 ASCIIEncoding 化一個備援為 或 EncoderFallbackExceptionDecoderFallbackException的物件,如下範例所示。
using System;
using System.Text;
public class Example
{
public static void Main()
{
Encoding enc = Encoding.GetEncoding("us-ascii",
new EncoderExceptionFallback(),
new DecoderExceptionFallback());
string value = "\u00C4 \u00F6 \u00AE";
try {
byte[] bytes= enc.GetBytes(value);
foreach (var byt in bytes)
Console.Write("{0:X2} ", byt);
Console.WriteLine();
string value2 = enc.GetString(bytes);
Console.WriteLine(value2);
}
catch (EncoderFallbackException e) {
Console.WriteLine("Unable to encode {0} at index {1}",
e.IsUnknownSurrogate() ?
String.Format("U+{0:X4} U+{1:X4}",
Convert.ToUInt16(e.CharUnknownHigh),
Convert.ToUInt16(e.CharUnknownLow)) :
String.Format("U+{0:X4}",
Convert.ToUInt16(e.CharUnknown)),
e.Index);
}
}
}
// The example displays the following output:
// Unable to encode U+00C4 at index 0
Imports System.Text
Module Example
Public Sub Main()
Dim enc As Encoding = Encoding.GetEncoding("us-ascii",
New EncoderExceptionFallback(),
New DecoderExceptionFallback())
Dim value As String = String.Format("{0} {1} {2}",
ChrW(&h00C4), ChrW(&h00F6), ChrW(&h00AE))
Try
Dim bytes() As Byte = enc.GetBytes(value)
For Each byt As Byte In bytes
Console.Write("{0:X2} ", byt)
Next
Console.WriteLine()
Dim value2 As String = enc.GetString(bytes)
Console.WriteLine(value2)
Catch e As EncoderFallbackException
Console.WriteLine("Unable to encode {0} at index {1}",
If(e.IsUnknownSurrogate(),
String.Format("U+{0:X4} U+{1:X4}",
Convert.ToUInt16(e.CharUnknownHigh),
Convert.ToUInt16(e.CharUnknownLow)),
String.Format("U+{0:X4}",
Convert.ToUInt16(e.CharUnknown))),
e.Index)
End Try
End Sub
End Module
' The example displays the following output:
' Unable to encode U+00C4 at index 0