EncodingStatic: GetEncoding

GetEncoding

Returns the encoding associated with the specified code page identifier or name. Optional parameters specify an error handler for characters that cannot be encoded and byte sequences that cannot be decoded.



 Public Function GetEncoding(
	  ByRef CodePageOrName As Variant,
	  Optional ByVal EncoderFallback As EncoderFallback,
	  Optional ByVal DecoderFallback As DecoderFallback ) As Encoding

Parameters

CodePageOrName
[ByRef] Variant. The code page number or name to encode with or 0 (zero) to use the default encoding.
EncoderFallback
[ByVal] Optional. EncoderFallback. An object that provides an error-handling procedure when a character cannot be encoded with the current encoding.
DecoderFallback
[ByVal] Optional. DecoderFallback. An object that provides an error-handling procedure when a byte sequence cannot be decoded with the current encoding.

Return Values

Encoding -  An encoding for a specific codepage or name.

Remarks

Note
Some unsupported code pages cause the exception ArgumentException to be thrown, whereas others cause NotSupportedException. Therefore, your code must catch all exceptions indicated in the Exceptions section.

The GetEncoding method relies on the underlying platform to support most code pages. However, VBCorLib natively supports some encodings.

Note
The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For this reason, we do not recommend encoding and decoding data by using the default code page returned by Encoding.GetEncoding(0). For the most consistent results, use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

To get the encoding associated with the default ANSI code page in the operating systems regional and language settings, you can either use a setting of 0 for the codepage parameter or use the Default property. To determine the default code pages used on the system, use the Windows GetSystemDefaultLangID function. To determine the current ANSI code page, use the Windows GetACP function.

GetEncoding returns a cached instance with default settings. The application should use the constructors of derived classes to get an instance with different settings. For example, the UTF32Encoding class provides a constructor that lets you enable error detection.

Exceptions

ExceptionCondition
ArgumentException CodePageOrName is not a valid code page.
-or-
The code page indicated by CodePageOrName is not supported by the underlying platform.

Examples

The following example demonstrates the Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) method.

Public Sub Main()
    Dim ae              As Encoding
    Dim InputString     As String
    Dim EncodedBytes()  As Byte
    Dim DecodedString   As String
    Dim NumBytes        As Long
    Dim b               As Variant
    Dim c               As Variant
    Dim ix              As Long
    
    Set Console.OutputEncoding = Encoding.UTF8
    
    ' Create an encoding, which is equivalent to calling the
    ' ASCIIEncoding class constructor.
    ' The EncoderReplacementFallback parameter specifies that the
    ' string, "(unknown)", replace characters that cannot be encoded.
    ' A decoder replacement fallback is also specified, but in this
    ' code example the decoding operation cannot fail.
    Set ae = Encoding.GetEncoding("us-ascii", NewEncoderReplacementFallback("(unknown)"), NewDecoderReplacementFallback("(error)"))
    
    ' The input string consists of the Unicode characters LEFT POINTING
    ' DOUBLE ANGLE QUOTATION MARK (U+00AB), 'X' (U+0058), and RIGHT POINTING
    ' DOUBLE ANGLE QUOTATION MARK (U+00BB).
    ' The encoding can only encode characters in the US-ASCII range of U+0000
    ' through U+007F. Consequently, the characters bracketing the 'X' character
    ' are replaced with the fallback replacement string, "(unknown)".
    InputString = t("\u00abX\u00bb")
    
    ReDim EncodedBytes(0 To ae.GetByteCount(InputString) - 1)
    
    ' Display the name of encoding.
    Console.WriteLine "The name of the encoding is ""{0}"".{1}", ae.WebName, vbCrLf
    
    ' Display the input string text.
    Console.WriteLine "Input string ({0} characters): ""{1}""", Len(InputString), InputString
    
    ' Display the input string in hexadecimal.
    Console.WriteValue "Input string in hexadecimal: "
        
    For Each c In CorString.ToCharArray(InputString)
        Console.WriteValue "0x{0:X2} ", c
    Next
    
    ' Encode the input string.
    Console.WriteLine "Encode the input string..."
    NumBytes = ae.GetBytesEx(InputString, 0, Len(InputString), EncodedBytes, 0)
    
    ' Display the encoded bytes.
    Console.WriteLine "Encoded bytes in hexadecimal ({0} bytes):{1}", NumBytes, vbCrLf
    
    For Each b In EncodedBytes
        Console.WriteValue "0x{0:X2} ", b
        ix = ix + 1
        
        If ix Mod 6 = 0 Then
            Console.WriteLine
        End If
    Next
    
    Console.WriteLine vbCrLf
    
    ' Decode the encoded bytes, yielding a reconstituted string.
    DecodedString = ae.GetString(EncodedBytes)
    
    ' Display then input string and the decoded string for comparison
    Console.WriteLine "Input string: ""{0}""", InputString
    Console.WriteLine "Decoded string: ""{0}""", DecodedString
    
    Console.ReadKey
End Sub

' This code produces the following output.
'
'    The name of the encoding is "us-ascii".
'
'    Input string (3 characters): "«X»"
'    Input string in hexadecimal: 0xAB 0x58 0xBB
'
'    Encode the input string...
'    Encoded bytes in hexadecimal (19 bytes):
'
'    0x28 0x75 0x6E 0x6B 0x6E 0x6F
'    0x77 0x6E 0x29 0x58 0x28 0x75
'    0x6E 0x6B 0x6E 0x6F 0x77 0x6E
'    0x29
'
'    Decode the encoded bytes...
'    Input string:  "«X»"
'    Decoded string:"(unknown)X(unknown)"

See Also

Project CorLib Overview

Class EncodingStatic Overview