Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Encoding.ASCII Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets an encoding for the ASCII (7-bit) character set.

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

public static Encoding ASCII { get; }

Property Value

Type: System.Text.Encoding

An encoding for the ASCII (7-bit) character set.

ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F.

When selecting the ASCII encoding for your app, consider the following:

  • The ASCII encoding is usually appropriate for protocols that require ASCII.

  • If you requires 8-bit encoding (which is sometimes incorrectly referred to as "ASCII"), the UTF-8 encoding is recommended over the 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 app. 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(String, EncoderFallback, DecoderFallback) method to instantiate an ASCIIEncoding object whose fallback is either an EncoderFallbackException or a DecoderFallbackException, as the following example illustrates.

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);

         string value2 = enc.GetString(bytes);
      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.CharUnknownLow)) :
// The example displays the following output:
//        Unable to encode U+00C4 at index 0

The following example demonstrates the effect of the ASCII encoding on characters that are outside the ASCII range.

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:");

      // 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("Encoded bytes:");
      foreach (Byte b in encodedBytes) 
          Console.Write("[{0}]", b);

      // Notice that the special characters have been replaced with
      // the value 63, which is the ASCII character code for '?'.
          "Value at position of Pi character: {0}",
          "Value at position of Sigma character: {0}",

      // Decode bytes back to a string.
      // Notice missing the Pi and Sigma characters.
      String decodedString = ascii.GetString(encodedBytes);
      Console.WriteLine("Decoded bytes:");
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:

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 (?).


Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
© 2018 Microsoft