Encoding::ASCII Property
Updated: October 2010
Gets an encoding for the ASCII (7-bit) character set.
Assembly: mscorlib (in mscorlib.dll)
ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F.
When selecting the ASCII encoding for your applications, consider the following:
The ASCII encoding is usually appropriate for protocols that require ASCII.
If your application requires 8-bit encoding (which is sometimes incorrectly referred to as "ASCII" encoding), UTF-8 encoding is recommended over ASCII encoding. For the characters 0-7F, the results are identical, but use of UTF-8 avoids data loss by allowing representation of all Unicode characters that are representable. Note that the ASCII encoding has an 8th bit ambiguity that can allow malicious use, but the UTF-8 encoding removes ambiguity about the 8th bit.
Prior to the .NET Framework version 2.0, the .NET Framework allowed spoofing by ignoring the 8th bit. Beginning with the .NET Framework 2.0, non-ASCII code points fall back during decoding.
The ASCIIEncoding object that is returned by this property might not have the appropriate behavior for your application. It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character. Instead, you can call the GetEncoding method to instantiate an ASCIIEncoding object whose fallback is either an EncoderFallbackException or a DecoderFallbackException, as the following example illustrates.
The following example demonstrates the effect of the ASCII encoding on characters that are outside the ASCII range.
using namespace System; using namespace System::Text; using namespace System::Collections; int main() { // Create an ASCII encoding. Encoding^ ascii = Encoding::ASCII; // 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 the positions of the special characters for later reference. int indexOfPi = unicodeString->IndexOf( L'\u03a0' ); int indexOfSigma = unicodeString->IndexOf( L'\u03a3' ); // Encode the 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( "[{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 the missing 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 (?). */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.