Encoding::GetEncoding Method (Int32)
Returns the encoding associated with the specified code page identifier.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- codepage
- Type: System::Int32
The code page identifier of the preferred encoding.
-or-
0, to use the default encoding.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | codepage is less than zero or greater than 65535. |
| ArgumentException | codepage is not supported by the underlying platform. |
| NotSupportedException | codepage 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. Alternatively, your application uses the GetEncodings method to get a list of all encodings.
To get the encoding associated with the default ANSI code page in the operating system's regional and language settings, the application can use a setting of 0 for the codepage parameter or the Default property. To determine the default code pages used on the system, the application uses the Windows API function GetSystemDefaultLangID. To determine the current ANSI code page, the application uses the Windows API function GetACP.
Note: |
|---|
Some unsupported code pages cause ArgumentException, while others cause NotSupportedException. Therefore, your code must catch all exceptions indicated in the Exceptions section. |
Note: |
|---|
The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications 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. 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 allows enabling of error detection.
The following example gets two instances of the same encoding (one by codepage and another by name), and checks their equality.
using namespace System; using namespace System::Text; int 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 */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: