EncodingStatic: ASCII (get)

ASCII

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



 Public Property Get ASCII ( ) As ASCIIEncoding

Return Values

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

Remarks

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 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.

Public Sub Main()
    Dim Enc     As Encoding
    Dim Value   As String
    Dim Value2  As String
    Dim Bytes() As Byte
    Dim Byt     As Variant
    
    Set Enc = Encoding.GetEncoding("us-ascii", New EncoderExceptionFallback, New DecoderExceptionFallback)
    Value = t("\u00C4 \u00F6 \u00AE")
    
    On Error GoTo Catch
    Bytes = Enc.GetBytes(Value)
    
    For Each Byt In Bytes
        Debug.Print Object.ToString(Byt, "X2")
    Next
    Debug.Print
    
    Value2 = Enc.GetString(Bytes)
    Debug.Print Value2
    Exit Sub
    
Catch:
    Dim Ex As EncoderFallbackException
    Catch Ex, Err
    
    Debug.Print CorString.Format("Unable to encode {0} at index {1}", IIf(Ex.CharUnknownHigh <> 0, _
                                CorString.Format("U+{0:X4} U+{1:X4}", Ex.CharUnknownHigh, Ex.CharUnknownLow), _
                                CorString.Format("U+{0:X4}", Ex.CharUnknown)), _
                                Ex.Index)
End Sub

' This example displays the following output:
'       Unable to encode U+00C4 at index 0

Read Only.

Examples

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

Public Sub Main()
    Dim ASCII           As Encoding
    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
    Set ASCII = Encoding.ASCII
    
    ' 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 the positions of the special characters for later reference.
    IndexOfPi = InStr(UnicodeString, ChrW$(&H3A0))
    IndexOfSigma = InStr(UnicodeString, ChrW$(&H3A3))
    
    ' Encode the 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 character: {0}", EncodedBytes(IndexOfPi - 1)
    Console.WriteLine "Value as position of Sigma character: {0}", EncodedBytes(IndexOfSigma - 1)
    
    ' Decode bytes back to a string.
    ' Notice missing the Pi and Sigma characters.
    DecodedString = ASCII.GetString(EncodedBytes)
    Console.WriteLine
    Console.WriteLine "Decoded bytes:"
    Console.WriteLine DecodedString
    Console.ReadKey
End Sub

' This example displays 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 (?).

See Also

Project CorLib Overview

Class EncodingStatic Overview