ASCIIEncoding

ASCIIEncoding


Provides a set of methods used to encode and decode chars to and from bytes in ASCII format.


Implements:

Encoding 
IObject 

Public:

Properties:

NameDescription
 BodyName (get) Gets the encoding name to be used in with the mail agent body tags.  
 CodePage (get) Gets the code page identifier for this encoding.  
 DecoderFallback (get) Gets the current DecoderFallback instance used by the encoding.  
 DecoderFallback (set) Sets the DecoderFallback to be used by this encoding instance.  
 EncoderFallback (get) Gets the current EncoderFallback instance used by the encoding.  
 EncoderFallback (set) Sets the EncoderFallback to be used by this encoding instance.  
 EncodingName (get) Gets the name of this encoding.  
 HeaderName (get) Gets the encoding name to be used in with the mail agent header tags.  
 IsBrowserDisplay (get) Gets if this encoding can be used by browsers to display text.  
 IsBrowserSave (get) Gets if this encoding can be used to save data with this encoding.  
 IsMailNewsDisplay (get) Gets if this encoding can be used to display mail and news by mail and news clients.  
 IsMailNewsSave (get) Gets if this encoding can be used to save date by mail and news clients.  
 IsReadOnly (get) When implemented in a derived class, gets a value indicating whether the current encoding is read-only.  
 IsSingleByte (get) Gets if the current encoding uses single-byte code points.  
 WebName (get) Gets the encoding name registered with the Internet Assigned Numbers Authority.  
 WindowsCodePage (get) Gets the Windows Operating Systems code page for this encoding.  

Methods:

NameDescription
 Clone Creates a clone of the current Encoding instance.  
 Equals Returns a boolean indicating if the value and this object instance are the same instance.  
 GetByteCount Calculates the number of bytes produced by encoding the characters in the specified string or character array.  
 GetBytes Encodes a set of characters into an array of bytes.  
 GetBytesEx Encodes a set of characters into an array of bytes, returning the number of bytes produced.  
 GetCharCount Returns the number of characters that would be produced by decoding a byte array.  
 GetChars Decodes a set of bytes into a set of characters.  
 GetCharsEx Decodes a set of bytes into the supplied Integer array.  
 GetDecoder Obtains a decoder that converts an ASCII encoded sequence of bytes into a sequence of Unicode characters.  
 GetEncoder Obtains an encoder that converts a sequence of Unicode characters into an ASCII encoded sequence of bytes.  
 GetHashCode Returns a pseudo-unique number identifying this instance.  
 GetMaxByteCount Returns the maximum number of bytes that can be created from a specific number of characters.  
 GetMaxCharCount Returns the maximum number of characters than can be decoded from the number of bytes specified.  
 GetPreamble Returns an array of bytes that represents this encoding.  
 GetString Decodes a range of bytes into a String.  
 ToString Returns a string representation of this object instance.  

Remarks

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. Because ASCII is a 7-bit encoding, ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F. If you use the default encoder returned by the Encoding.ASCII property or the ASCIIEncoding constructor, characters outside that range are replaced with a question mark (?) before the encoding operation is performed. Because the ASCIIEncoding class supports only a limited character set, the UTF8Encoding, UnicodeEncoding, and UTF32Encoding classes are better suited for globalized applications. The following considerations can help you to decide whether to use ASCIIEncoding:

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.

Examples

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.

Public Sub Main()
    Dim ASCII           As New ASCIIEncoding
    Dim UnicodeString   As String
    Dim IndexOfPi       As Long
    Dim IndexOfSigma    As Long
    Dim EncodedBytes()  As Byte
    Dim DecodedString   As String
    Dim b               As Variant
    
    Set Console.OutputEncoding = Encoding.UTF8
    
    ' A Unicode string with two characters outside the ASCII code range.
    UnicodeString = t("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.
    IndexOfPi = InStr(UnicodeString, ChrW$(&H3A0))
    IndexOfSigma = InStr(UnicodeString, ChrW$(&H3A3))
    
    ' Encode string
    EncodedBytes = ASCII.GetBytes(UnicodeString)
    Console.WriteLine
    Console.WriteLine "Encoded bytes:"
    
    For Each b In EncodedBytes
        Console.WriteValue "[{0}]", b
    Next
    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 as position of Pi characters: {0}", EncodedBytes(IndexOfPi - 1)
    Console.WriteLine "Value as position of Sigma character: {0}", EncodedBytes(IndexOfSigma - 1)
    
    ' Decode bytes back to string.
    ' Notice missing Pi and Sigma characters.
    DecodedString = ASCII.GetString(EncodedBytes)
    Console.WriteLine
    Console.WriteLine "Decoded bytes:"
    Console.WriteLine DecodedString
    Console.ReadKey
End Sub

See Also

Project CorLib Overview

Class ASCIIEncoding Overview

Encoding

UTF8Encoding

UTF7Encoding

UTF32Encoding

UnicodeEncoding