Export (0) Print
Expand All

ASCIIEncoding Class

Represents an ASCII character encoding of Unicode characters.

For a list of all members of this type, see ASCIIEncoding Members.

System.Object
   System.Text.Encoding
      System.Text.ASCIIEncoding

[Visual Basic]
<Serializable>
Public Class ASCIIEncoding
   Inherits Encoding
[C#]
[Serializable]
public class ASCIIEncoding : Encoding
[C++]
[Serializable]
public __gc class ASCIIEncoding : public Encoding
[JScript]
public
   Serializable
class ASCIIEncoding extends Encoding

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. This limitation makes ASCII largely inadequate for internationalized applications. Consider using UTF8Encoding or UnicodeEncoding instead.

The Windows code page that corresponds to ASCIIEncoding is 20127.

This class inherits from the Encoding class.

Example

[Visual Basic, C#, C++] The following example demonstrates how to encode Unicode characters as ASCII characters. Notice the loss of data that occurs when you use ASCIIEncoding to encode Unicode characters outside of the ASCII range.

[Visual Basic] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings

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 'Main
End Class 'ASCIIEncodingExample

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Text; 

int main()
{
   // The encoding.
   ASCIIEncoding* ascii = new ASCIIEncoding();

   // A Unicode string with two characters outside the ASCII code range.
   String * unicodeString =
      S"This Unicode String* contains two characters with codes outside the ASCII code range, Pi (\u03a0) and Sigma (\u03a3).";

   Console::WriteLine(S"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.
   Byte encodedBytes[] = ascii -> GetBytes(unicodeString);
   Console::WriteLine();
   Console::WriteLine(S"Encoded bytes:");

   IEnumerator* myEnum = encodedBytes->GetEnumerator();
   while (myEnum->MoveNext()) 
   {
      Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
      Console::Write(S"->Item[ {0}]", __box(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(S"Value at position of Pi character: {0}",
      encodedBytes->Item[indexOfPi]);

   Console::WriteLine(S"Value at position of Sigma character: {0}",
      encodedBytes->Item[indexOfSigma]);

   // Decode bytes back to string.
   // Notice missing Pi and Sigma characters.
   String * decodedString = ascii -> GetString(encodedBytes);
   Console::WriteLine();
   Console::WriteLine(S"Decoded bytes:");
   Console::WriteLine(decodedString);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Text

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ASCIIEncoding Members | System.Text Namespace

Show:
© 2014 Microsoft