ASCIIEncoding Class
Updated: March 2011
Represents an ASCII character encoding of Unicode characters.
Assembly: mscorlib (in mscorlib.dll)
Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters.
ASCIIEncoding corresponds to the Windows code page 20127. ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F. For more information about the encodings supported by System.Text, see Understanding Encodings and Using Unicode Encoding.
Caution: |
|---|
ASCIIEncoding does not provide error detection. For security reasons, you should use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection. |
The GetByteCount method determines how many bytes result in encoding a set of Unicode characters, and the GetBytes method performs the actual encoding.
Likewise, the GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetChars and GetString methods perform the actual decoding.
Note that the default ASCIIEncoding constructor by itself might not have the appropriate behavior for your application. You might want to consider setting the EncoderFallback or DecoderFallback property to EncoderExceptionFallback or DecoderExceptionFallback to prevent sequences with the 8th bit set. Custom behavior might also be appropriate for these cases.
The following example demonstrates how to encode Unicode characters into ASCII. Notice the loss of data that occurs when your application uses ASCIIEncoding to encode Unicode characters outside of the ASCII range.
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
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.
Caution: