Encoding.GetEncoding Method (String, EncoderFallback, DecoderFallback)

Note: This method is new in the .NET Framework version 2.0.

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

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

static Encoding^ GetEncoding (
	String^ name, 
	EncoderFallback^ encoderFallback, 
	DecoderFallback^ decoderFallback
public static Encoding GetEncoding (
	String name, 
	EncoderFallback encoderFallback, 
	DecoderFallback decoderFallback
public static function GetEncoding (
	name : String, 
	encoderFallback : EncoderFallback, 
	decoderFallback : DecoderFallback
) : Encoding



The code page name of the preferred encoding.


An EncoderFallback object that provides an error handling procedure when a character cannot be encoded with the current encoding.


A DecoderFallback object that provides an error handling procedure when a byte sequence cannot be decoded with the current encoding.

Return Value

The Encoding object associated with the specified code page.

Exception typeCondition


name is not a valid code page name.


The code page indicated by name is not supported by the underlying platform.

The GetEncoding method relies on the underlying platform to support most code pages; however, the .NET Framework natively supports some encodings.

For a list of code pages, see the Encoding class topic. Or use the GetEncodings method to get a list of all encodings.

To get the encoding associated with the default ANSI code page in the system's regional settings, use GetEncoding(0) or the Default property. To determine the default code pages used on the system, use the Windows API GetSystemDefaultLangID. To determine the current ANSI code page, use the Windows API GetACP.

Specify a null reference (Nothing in Visual Basic) for the encoderFallback or decoderFallback parameters to obtain the default encoder and decoder fallbacks for the current encoding. The default encoder and decoder fallbacks substitute a close equivalent, which is known as the best fit, for a character that cannot be encoded or a byte sequence that cannot be decoded.

GetEncoding returns a cached instance with default settings. Use the constructors of derived classes to get an instance with different settings; for example, the UTF32Encoding class provides a constructor that allows you to enable error detection.

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

// This example demonstrates the EncoderReplacementFallback class.

using namespace System;
using namespace System::Text;

int main()
    // 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.

    Encoding^ ascii = Encoding::GetEncoding("us-ascii",
        gcnew EncoderReplacementFallback("(unknown)"),
        gcnew DecoderReplacementFallback("(error)"));

    // The input string consists of the Unicode characters LEFT POINTING
    // DOUBLE ANGLE QUOTATION MARK (U+00AB), 'X' (U+0058), and RIGHT
    // 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)".

    String^ inputString = "\u00abX\u00bb";
    String^ decodedString;
    String^ twoNewLines = Environment::NewLine + Environment::NewLine;
    array <Byte>^ encodedBytes = 
        gcnew array<Byte>(ascii->GetByteCount(inputString));
    int numberOfEncodedBytes = 0;

    // ---------------------------------------------------------------------

    // Display the name of the encoding.
    Console::WriteLine("The name of the encoding is \"{0}\".{1}", 
        ascii->WebName, Environment::NewLine);

    // Display the input string in text.
    Console::WriteLine("Input string ({0} characters): \"{1}\"",
        inputString->Length, inputString);

    // Display the input string in hexadecimal.
    Console::Write("Input string in hexadecimal: ");
    for each (char c in inputString)
        Console::Write("0x{0:X2} ", c);

    // ---------------------------------------------------------------------
    // Encode the input string.

    Console::WriteLine("Encode the input string...");
    numberOfEncodedBytes = ascii->GetBytes(inputString, 0, inputString->Length,
        encodedBytes, 0);

    // Display the encoded bytes.
    Console::WriteLine("Encoded bytes in hexadecimal ({0} bytes):{1}",
        numberOfEncodedBytes, Environment::NewLine);
    for(int i = 0; i < encodedBytes->Length; i++)
        Console::Write("0x{0:X2} ", encodedBytes[i]);
        if(((i + 1) % 6) == 0)

    // ---------------------------------------------------------------------
    // Decode the encoded bytes, yielding a reconstituted string.

    Console::WriteLine("Decode the encoded bytes...");
    decodedString = ascii->GetString(encodedBytes);

    // Display the input string and the decoded string for comparison.
    Console::WriteLine("Input string:  \"{0}\"", inputString);
    Console::WriteLine("Decoded string:\"{0}\"", decodedString);

This code example produces the following results:

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

Decode the encoded bytes...
Input string:  "X"
Decoded string:"(unknown)X(unknown)"


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions