Encoding.GetEncoding Method (Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns the encoding associated with the specified code page identifier.

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

public static Encoding GetEncoding(
	int codepage


Type: System.Int32

The code page identifier of the preferred encoding. Possible values are listed in the Code Page column of the table that appears in the Encoding class topic.


0 (zero), to use the default encoding.

Return Value

Type: System.Text.Encoding

The encoding that is associated with the specified code page.

Exception Condition

codepage is less than zero or greater than 65535.


codepage is not supported by the underlying platform.


codepage is not supported by the underlying platform.

The fallback handler depends on the encoding type of codepage. If codepage is a code page or double-byte character set (DBCS) encoding, a best-fit fallback handler is used. Otherwise, a replacement fallback handler is used. These fallback handlers may not be appropriate for your app. To specify the fallback handler used by the encoding specified by codepage, you can call the GetEncoding(Int32, EncoderFallback, DecoderFallback) overload.

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. Alternatively, you can call the GetEncodings method to get an array of EncodingInfo objects that contains information about all encodings.

In addition to the encodings that are intrinsically supported on a specific platform version of the .NET Framework, the GetEncoding(Int32) method returns any additional encodings that are made available by registering an EncodingProvider object.

To get the encoding associated with the default ANSI code page in the operating system's regional and language settings, you can either supply a value 0 for the codepage argument or, on the full .NET Framework on the Windows desktop, retrieve the value of 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, call the Windows GetACP function from the full .NET Framework on the Windows desktop.


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 ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For this reason, encoding and decoding data using the default code page returned by Encoding.GetEncoding(0) is not recommended. For the most consistent results, you should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

GetEncoding returns a cached instance with default settings. You 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.

The following example gets two instances of the same encoding (one by code page and another by name), and checks their equality.

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Get a UTF-32 encoding by codepage.
      Encoding e1 = Encoding.GetEncoding( 12000 );

      // Get a UTF-32 encoding by name.
      Encoding e2 = Encoding.GetEncoding( "utf-32" );

      // Check their equality.
      Console.WriteLine( "e1 equals e2? {0}", e1.Equals( e2 ) );



This code produces the following output.

e1 equals e2? True


Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top