Encoding.GetEncoding 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 코드 페이지에 대한 인코딩을 반환합니다.
오버로드
| Name | Description |
|---|---|
| GetEncoding(Int32) |
지정된 코드 페이지 식별자와 연결된 인코딩을 반환합니다. |
| GetEncoding(String) |
지정된 코드 페이지 이름과 연결된 인코딩을 반환합니다. |
| GetEncoding(Int32, EncoderFallback, DecoderFallback) |
지정된 코드 페이지 식별자와 연결된 인코딩을 반환합니다. 매개 변수는 인코딩할 수 없는 문자와 디코딩할 수 없는 바이트 시퀀스에 대한 오류 처리기를 지정합니다. |
| GetEncoding(String, EncoderFallback, DecoderFallback) |
지정된 코드 페이지 이름과 연결된 인코딩을 반환합니다. 매개 변수는 인코딩할 수 없는 문자와 디코딩할 수 없는 바이트 시퀀스에 대한 오류 처리기를 지정합니다. |
GetEncoding(Int32)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
지정된 코드 페이지 식별자와 연결된 인코딩을 반환합니다.
public:
static System::Text::Encoding ^ GetEncoding(int codepage);
public static System.Text.Encoding GetEncoding(int codepage);
static member GetEncoding : int -> System.Text.Encoding
Public Shared Function GetEncoding (codepage As Integer) As Encoding
매개 변수
반환
지정된 코드 페이지와 연결된 인코딩입니다.
예외
codepage 가 0보다 작거나 65535보다 큽니다.
codepage 는 기본 플랫폼에서 지원되지 않습니다.
codepage 는 기본 플랫폼에서 지원되지 않습니다.
예제
다음 예제에서는 동일한 인코딩의 두 인스턴스(코드 페이지와 이름으로 하나씩)를 가져오고 해당 인스턴스의 같음을 확인합니다.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' Get a UTF-32 encoding by codepage.
Dim e1 As Encoding = Encoding.GetEncoding(12000)
' Get a UTF-32 encoding by name.
Dim e2 As Encoding = Encoding.GetEncoding("utf-32")
' Check their equality.
Console.WriteLine("e1 equals e2? {0}", e1.Equals(e2))
End Sub
End Class
'This code produces the following output.
'
'e1 equals e2? True
설명
대체 처리기는 인코딩 형식 codepage에 따라 달라집니다. 코드 페이지 또는 DBCS(더블 바이트 문자 집합) 인코딩인 경우 codepage 가장 적합한 대체 처리기가 사용됩니다. 그렇지 않으면 대체 대체 처리기가 사용됩니다. 이러한 대체 처리기는 앱에 적합하지 않을 수 있습니다. 지정 codepage한 인코딩에서 사용하는 대체 처리기를 지정하려면 오버로드를 호출할 GetEncoding(Int32, EncoderFallback, DecoderFallback) 수 있습니다.
.NET Framework에서 이 메서드는 GetEncoding 기본 플랫폼을 사용하여 대부분의 코드 페이지를 지원합니다. 그러나 .NET Framework는 기본적으로 일부 인코딩을 지원합니다. 코드 페이지 목록은 인코딩 목록을 참조하세요. .NET Core에서 이 메서드는 GetEncoding .NET Core에서 기본적으로 지원되는 인코딩을 반환합니다. 두 .NET 구현 모두에서 메서드를 호출 GetEncodings 하여 사용 가능한 모든 인코딩에 대한 정보를 포함하는 개체 배열 EncodingInfo 을 가져올 수 있습니다.
.NET Core에서 기본적으로 사용할 수 있거나 특정 플랫폼 버전의 .NET Framework GetEncoding 에서 기본적으로 지원되는 인코딩 외에도 이 메서드는 개체를 등록하여 사용할 수 있는 추가 인코딩을 EncodingProvider 반환합니다. 동일한 인코딩이 여러 EncodingProvider 개체에 의해 등록된 경우 이 메서드는 마지막으로 등록된 인코딩을 반환합니다.
인수에 대해 0 codepage 값을 제공할 수도 있습니다. 동작은 .NET Framework와 .NET Core 이상 버전 간에 다릅니다.
.NET Framework: 항상 Windows에서 시스템의 활성 코드 페이지에 해당하는 인코딩을 반환합니다. 속성에서 반환하는 것과 동일한 인코딩입니다 Encoding.Default .
.NET Core 이상 버전: 동작은 애플리케이션의 인코딩 구성에 따라 달라집니다.
인코딩 공급자가 등록되지 않음: 와 동일한 Encoding.Default값을 반환합니다UTF8Encoding.
CodePagesEncodingProvider 등록됨:
- Windows에서 시스템의 활성 코드 페이지(.NET Framework 동작과 동일)와 일치하는 인코딩을 반환합니다.
- Windows가 아닌 플랫폼에서는 항상 .를 UTF8Encoding반환합니다.
등록된 다른 공급자: 동작은 해당 공급자에 의해 결정됩니다. 자세한 내용은 설명서를 참조하세요. 여러 공급자가 등록된 경우 메서드는 인수 0을 처리하는 마지막으로 등록된 공급자에서 인코딩을
codepage반환합니다.
메모
- 지원되지 않는 코드 페이지 중 일부는 throw되는 반면, 다른 코드 페이지는 ArgumentException throw됩니다 NotSupportedException. 따라서 코드는 예외 섹션에 표시된 모든 예외를 catch해야 합니다.
- .NET 5 이상 버전에서는 UTF-7을 나타내는 코드 페이지 식별자가
65000지원되지 않습니다.
메모
ANSI 코드 페이지는 컴퓨터별로 다를 수 있으며 단일 컴퓨터에서 변경되어 데이터가 손상될 수 있습니다. 따라서 활성 코드 페이지가 ANSI 코드 페이지인 경우 반환 Encoding.GetEncoding(0) 된 기본 코드 페이지를 사용하여 데이터를 인코딩하고 디코딩하는 것은 권장되지 않습니다. 가장 일관된 결과를 얻으려면 특정 코드 페이지 대신 UTF-8(코드 페이지 65001) 또는 UTF-16과 같은 유니코드 인코딩을 사용해야 합니다.
GetEncoding 는 기본 설정을 사용하여 캐시된 인스턴스를 반환합니다. 파생 클래스의 생성자를 사용하여 다른 설정의 인스턴스를 가져와야 합니다. 예를 들어 클래스는 UTF32Encoding 오류 검색을 사용하도록 설정할 수 있는 생성자를 제공합니다.
추가 정보
적용 대상
GetEncoding(String)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
지정된 코드 페이지 이름과 연결된 인코딩을 반환합니다.
public:
static System::Text::Encoding ^ GetEncoding(System::String ^ name);
public static System.Text.Encoding GetEncoding(string name);
static member GetEncoding : string -> System.Text.Encoding
Public Shared Function GetEncoding (name As String) As Encoding
매개 변수
반환
지정된 코드 페이지와 연결된 인코딩입니다.
예외
예제
다음 예제에서는 동일한 인코딩의 두 인스턴스(코드 페이지와 이름으로 하나씩)를 가져오고 해당 인스턴스의 같음을 확인합니다.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' Get a UTF-32 encoding by codepage.
Dim e1 As Encoding = Encoding.GetEncoding(12000)
' Get a UTF-32 encoding by name.
Dim e2 As Encoding = Encoding.GetEncoding("utf-32")
' Check their equality.
Console.WriteLine("e1 equals e2? {0}", e1.Equals(e2))
End Sub
End Class
'This code produces the following output.
'
'e1 equals e2? True
설명
대체 처리기는 인코딩 형식 name에 따라 달라집니다. 코드 페이지 또는 DBCS(더블 바이트 문자 집합) 인코딩인 경우 name 가장 적합한 대체 처리기가 사용됩니다. 그렇지 않으면 대체 대체 처리기가 사용됩니다. 이러한 대체 처리기는 앱에 적합하지 않을 수 있습니다. 지정 name한 인코딩에서 사용하는 대체 처리기를 지정하려면 오버로드를 호출할 GetEncoding(String, EncoderFallback, DecoderFallback) 수 있습니다.
.NET Framework에서 이 메서드는 GetEncoding 기본 플랫폼을 사용하여 대부분의 코드 페이지를 지원합니다. 그러나 .NET Framework는 기본적으로 일부 인코딩을 지원합니다. 코드 페이지 목록은 인코딩 목록을 참조하세요. .NET Core에서 이 메서드는 GetEncoding .NET Core에서 기본적으로 지원되는 인코딩을 반환합니다. 두 .NET 구현 모두에서 메서드를 호출 GetEncodings 하여 사용 가능한 모든 인코딩에 대한 정보를 포함하는 개체 배열 EncodingInfo 을 가져올 수 있습니다.
.NET Core에서 기본적으로 사용할 수 있거나 특정 플랫폼 버전의 .NET Framework GetEncoding 에서 기본적으로 지원되는 인코딩 외에도 이 메서드는 개체를 등록하여 사용할 수 있는 추가 인코딩을 EncodingProvider 반환합니다. 동일한 인코딩이 여러 EncodingProvider 개체에 의해 등록된 경우 이 메서드는 마지막으로 등록된 인코딩을 반환합니다.
.NET 5 이상 버전에서는 코드 페이지 이름이 utf-7 지원되지 않습니다.
메모
ANSI 코드 페이지는 다른 컴퓨터에서 다를 수 있습니다. 또는 단일 컴퓨터에 대해 변경되어 데이터가 손상될 수 있습니다. 가장 일관된 결과를 얻으려면 특정 코드 페이지 대신 UTF-8(코드 페이지 65001) 또는 UTF-16과 같은 유니코드를 사용합니다.
GetEncoding 는 기본 설정을 사용하여 캐시된 인스턴스를 반환합니다. 파생 클래스의 생성자를 사용하여 다른 설정의 인스턴스를 가져와야 합니다. 예를 들어 클래스는 UTF32Encoding 오류 검색을 사용하도록 설정할 수 있는 생성자를 제공합니다.
추가 정보
적용 대상
GetEncoding(Int32, EncoderFallback, DecoderFallback)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
지정된 코드 페이지 식별자와 연결된 인코딩을 반환합니다. 매개 변수는 인코딩할 수 없는 문자와 디코딩할 수 없는 바이트 시퀀스에 대한 오류 처리기를 지정합니다.
public:
static System::Text::Encoding ^ GetEncoding(int codepage, System::Text::EncoderFallback ^ encoderFallback, System::Text::DecoderFallback ^ decoderFallback);
public static System.Text.Encoding GetEncoding(int codepage, System.Text.EncoderFallback encoderFallback, System.Text.DecoderFallback decoderFallback);
static member GetEncoding : int * System.Text.EncoderFallback * System.Text.DecoderFallback -> System.Text.Encoding
Public Shared Function GetEncoding (codepage As Integer, encoderFallback As EncoderFallback, decoderFallback As DecoderFallback) As Encoding
매개 변수
- encoderFallback
- EncoderFallback
문자를 현재 인코딩으로 인코딩할 수 없는 경우 오류 처리 프로시저를 제공하는 개체입니다.
- decoderFallback
- DecoderFallback
바이트 시퀀스를 현재 인코딩으로 디코딩할 수 없는 경우 오류 처리 프로시저를 제공하는 개체입니다.
반환
지정된 코드 페이지와 연결된 인코딩입니다.
예외
codepage 가 0보다 작거나 65535보다 큽니다.
codepage 는 기본 플랫폼에서 지원되지 않습니다.
codepage 는 기본 플랫폼에서 지원되지 않습니다.
예제
다음 예제에서는 메서드를 보여 줍니다 Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) .
// This example demonstrates the EncoderReplacementFallback class.
using System;
using System.Text;
class Sample
{
public static void 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 ae = Encoding.GetEncoding(
"us-ascii",
new EncoderReplacementFallback("(unknown)"),
new DecoderReplacementFallback("(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)".
string inputString = "\u00abX\u00bb";
string decodedString;
string twoNewLines = "\n\n";
byte[] encodedBytes = new byte[ae.GetByteCount(inputString)];
int numberOfEncodedBytes = 0;
int ix = 0;
// --------------------------------------------------------------------------
// Display the name of the encoding.
Console.WriteLine("The name of the encoding is \"{0}\".\n", ae.WebName);
// 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: ");
foreach (char c in inputString.ToCharArray())
{
Console.Write("0x{0:X2} ", (int)c);
}
Console.Write(twoNewLines);
// --------------------------------------------------------------------------
// Encode the input string.
Console.WriteLine("Encode the input string...");
numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length,
encodedBytes, 0);
// Display the encoded bytes.
Console.WriteLine("Encoded bytes in hexadecimal ({0} bytes):\n",
numberOfEncodedBytes);
ix = 0;
foreach (byte b in encodedBytes)
{
Console.Write("0x{0:X2} ", (int)b);
ix++;
if (0 == ix % 6) Console.WriteLine();
}
Console.Write(twoNewLines);
// --------------------------------------------------------------------------
// Decode the encoded bytes, yielding a reconstituted string.
Console.WriteLine("Decode the encoded bytes...");
decodedString = ae.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
0x29
Decode the encoded bytes...
Input string: "«X»"
Decoded string:"(unknown)X(unknown)"
*/
' This example demonstrates the EncoderReplacementFallback class.
Imports System.Text
Class Sample
Public Shared Sub 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.
Dim erf As New EncoderReplacementFallback("(unknown)")
Dim drf As New DecoderReplacementFallback("(error)")
Dim ae As Encoding = Encoding.GetEncoding("us-ascii", erf, drf)
' 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)".
Dim inputString As String = "«X»"
Dim decodedString As String
Dim twoNewLines As String = vbCrLf & vbCrLf
Dim ix As Integer = 0
Dim numberOfEncodedBytes As Integer = ae.GetByteCount(inputString)
' Counteract the compiler adding an extra byte to the array.
Dim encodedBytes(numberOfEncodedBytes - 1) As Byte
' --------------------------------------------------------------------------
' Display the name of the encoding.
Console.WriteLine("The name of the encoding is ""{0}""." & vbCrLf, ae.WebName)
' Display the input string in text.
Console.WriteLine("Input string ({0} characters): ""{1}""", _
inputString.Length, inputString)
' Display the input string in hexadecimal.
' Each element is converted to an integer with Convert.ToInt32.
Console.Write("Input string in hexadecimal: ")
Dim c As Char
For Each c In inputString.ToCharArray()
Console.Write("0x{0:X2} ", Convert.ToInt32(c))
Next c
Console.Write(twoNewLines)
' --------------------------------------------------------------------------
' Encode the input string.
Console.WriteLine("Encode the input string...")
numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length, _
encodedBytes, 0)
' Display the encoded bytes.
' Each element is converted to an integer with Convert.ToInt32.
Console.WriteLine("Encoded bytes in hexadecimal ({0} bytes):" & vbCrLf, _
numberOfEncodedBytes)
ix = 0
Dim b As Byte
For Each b In encodedBytes
Console.Write("0x{0:X2} ", Convert.ToInt32(b))
ix += 1
If 0 = ix Mod 6 Then
Console.WriteLine()
End If
Next b
Console.Write(twoNewLines)
' --------------------------------------------------------------------------
' Decode the encoded bytes, yielding a reconstituted string.
Console.WriteLine("Decode the encoded bytes...")
decodedString = ae.GetString(encodedBytes)
' Display the input string and the decoded string for comparison.
Console.WriteLine("Input string: ""{0}""", inputString)
Console.WriteLine("Decoded string:""{0}""", decodedString)
End Sub
End Class
'
'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
'0x29
'
'Decode the encoded bytes...
'Input string: "X"
'Decoded string:"(unknown)X(unknown)"
'
설명
메모
- 지원되지 않는 일부 코드 페이지에서는 예외가 throw되는 반면, 다른 코드 페이지는 예외 ArgumentException 를 발생합니다 NotSupportedException. 따라서 코드는 예외 섹션에 표시된 모든 예외를 catch해야 합니다.
- .NET 5 이상 버전에서는 UTF-7을 나타내는 코드 페이지 식별자가
65000지원되지 않습니다.
.NET Framework에서 이 메서드는 GetEncoding 기본 플랫폼을 사용하여 대부분의 코드 페이지를 지원합니다. 그러나 .NET Framework는 기본적으로 일부 인코딩을 지원합니다. 코드 페이지 목록은 인코딩 목록을 참조하세요. .NET Core에서 이 메서드는 GetEncoding .NET Core에서 기본적으로 지원되는 인코딩을 반환합니다. 두 .NET 구현 모두에서 메서드를 호출 GetEncodings 하여 사용 가능한 모든 인코딩에 대한 정보를 포함하는 개체 배열 EncodingInfo 을 가져올 수 있습니다.
.NET Core에서 기본적으로 사용할 수 있거나 특정 플랫폼 버전의 .NET Framework GetEncoding 에서 기본적으로 지원되는 인코딩 외에도 이 메서드는 개체를 등록하여 사용할 수 있는 추가 인코딩을 EncodingProvider 반환합니다. 동일한 인코딩이 여러 EncodingProvider 개체에 의해 등록된 경우 이 메서드는 마지막으로 등록된 인코딩을 반환합니다.
인수에 대해 0 codepage 값을 제공할 수도 있습니다. 동작은 .NET Framework와 .NET Core 이상 버전 간에 다릅니다.
.NET Framework: 항상 Windows에서 시스템의 활성 코드 페이지에 해당하는 인코딩을 반환합니다. 속성에서 반환하는 것과 동일한 인코딩입니다 Encoding.Default .
.NET Core 이상 버전: 동작은 애플리케이션의 인코딩 구성에 따라 달라집니다.
인코딩 공급자가 등록되지 않음: 와 동일한 Encoding.Default값을 반환합니다UTF8Encoding.
CodePagesEncodingProvider 등록됨:
- Windows에서 시스템의 활성 코드 페이지(.NET Framework 동작과 동일)와 일치하는 인코딩을 반환합니다.
- Windows가 아닌 플랫폼에서는 항상 .를 UTF8Encoding반환합니다.
등록된 다른 공급자: 동작은 해당 공급자에 의해 결정됩니다. 자세한 내용은 설명서를 참조하세요. 여러 공급자가 등록된 경우 메서드는 인수 0을 처리하는 마지막으로 등록된 공급자에서 인코딩을
codepage반환합니다.
메모
ANSI 코드 페이지는 컴퓨터별로 다를 수 있으며 단일 컴퓨터에서 변경되어 데이터가 손상될 수 있습니다. 따라서 활성 코드 페이지가 ANSI 코드 페이지인 경우 반환 Encoding.GetEncoding(0) 된 기본 코드 페이지를 사용하여 데이터를 인코딩하고 디코딩하는 것은 권장되지 않습니다. 가장 일관된 결과를 얻으려면 특정 코드 페이지 대신 UTF-8(코드 페이지 65001) 또는 UTF-16과 같은 유니코드를 사용해야 합니다.
활성 코드 페이지와 연결된 인코딩을 얻으려면 인수에 대해 codepage 0 값을 제공하거나 코드가 .NET Framework에서 실행되는 경우 속성 값을 Encoding.Default 검색할 수 있습니다. 현재 활성 코드 페이지를 확인하려면 .NET Framework에서 Windows GetACP 함수를 호출합니다.
GetEncoding 는 기본 설정을 사용하여 캐시된 인스턴스를 반환합니다. 파생 클래스의 생성자를 사용하여 다른 설정의 인스턴스를 가져와야 합니다. 예를 들어 클래스는 UTF32Encoding 오류 검색을 사용하도록 설정할 수 있는 생성자를 제공합니다.
추가 정보
적용 대상
GetEncoding(String, EncoderFallback, DecoderFallback)
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
- Source:
- Encoding.cs
지정된 코드 페이지 이름과 연결된 인코딩을 반환합니다. 매개 변수는 인코딩할 수 없는 문자와 디코딩할 수 없는 바이트 시퀀스에 대한 오류 처리기를 지정합니다.
public:
static System::Text::Encoding ^ GetEncoding(System::String ^ name, System::Text::EncoderFallback ^ encoderFallback, System::Text::DecoderFallback ^ decoderFallback);
public static System.Text.Encoding GetEncoding(string name, System.Text.EncoderFallback encoderFallback, System.Text.DecoderFallback decoderFallback);
static member GetEncoding : string * System.Text.EncoderFallback * System.Text.DecoderFallback -> System.Text.Encoding
Public Shared Function GetEncoding (name As String, encoderFallback As EncoderFallback, decoderFallback As DecoderFallback) As Encoding
매개 변수
- name
- String
기본 설정 인코딩의 코드 페이지 이름입니다. 속성에서 반환된 WebName 모든 값이 유효합니다. 가능한 값은 클래스 항목에 표시되는 테이블의 이름 열에 Encoding 나열됩니다.
- encoderFallback
- EncoderFallback
문자를 현재 인코딩으로 인코딩할 수 없는 경우 오류 처리 프로시저를 제공하는 개체입니다.
- decoderFallback
- DecoderFallback
바이트 시퀀스를 현재 인코딩으로 디코딩할 수 없는 경우 오류 처리 프로시저를 제공하는 개체입니다.
반환
지정된 코드 페이지와 연결된 인코딩입니다.
예외
예제
다음 예제에서는 메서드를 보여 줍니다 Encoding.GetEncoding(String, EncoderFallback, DecoderFallback) .
// This example demonstrates the EncoderReplacementFallback class.
using System;
using System.Text;
class Sample
{
public static void 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 ae = Encoding.GetEncoding(
"us-ascii",
new EncoderReplacementFallback("(unknown)"),
new DecoderReplacementFallback("(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)".
string inputString = "\u00abX\u00bb";
string decodedString;
string twoNewLines = "\n\n";
byte[] encodedBytes = new byte[ae.GetByteCount(inputString)];
int numberOfEncodedBytes = 0;
int ix = 0;
// --------------------------------------------------------------------------
// Display the name of the encoding.
Console.WriteLine("The name of the encoding is \"{0}\".\n", ae.WebName);
// 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: ");
foreach (char c in inputString.ToCharArray())
{
Console.Write("0x{0:X2} ", (int)c);
}
Console.Write(twoNewLines);
// --------------------------------------------------------------------------
// Encode the input string.
Console.WriteLine("Encode the input string...");
numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length,
encodedBytes, 0);
// Display the encoded bytes.
Console.WriteLine("Encoded bytes in hexadecimal ({0} bytes):\n",
numberOfEncodedBytes);
ix = 0;
foreach (byte b in encodedBytes)
{
Console.Write("0x{0:X2} ", (int)b);
ix++;
if (0 == ix % 6) Console.WriteLine();
}
Console.Write(twoNewLines);
// --------------------------------------------------------------------------
// Decode the encoded bytes, yielding a reconstituted string.
Console.WriteLine("Decode the encoded bytes...");
decodedString = ae.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
0x29
Decode the encoded bytes...
Input string: "«X»"
Decoded string:"(unknown)X(unknown)"
*/
' This example demonstrates the EncoderReplacementFallback class.
Imports System.Text
Class Sample
Public Shared Sub 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.
Dim erf As New EncoderReplacementFallback("(unknown)")
Dim drf As New DecoderReplacementFallback("(error)")
Dim ae As Encoding = Encoding.GetEncoding("us-ascii", erf, drf)
' 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)".
Dim inputString As String = "«X»"
Dim decodedString As String
Dim twoNewLines As String = vbCrLf & vbCrLf
Dim ix As Integer = 0
Dim numberOfEncodedBytes As Integer = ae.GetByteCount(inputString)
' Counteract the compiler adding an extra byte to the array.
Dim encodedBytes(numberOfEncodedBytes - 1) As Byte
' --------------------------------------------------------------------------
' Display the name of the encoding.
Console.WriteLine("The name of the encoding is ""{0}""." & vbCrLf, ae.WebName)
' Display the input string in text.
Console.WriteLine("Input string ({0} characters): ""{1}""", _
inputString.Length, inputString)
' Display the input string in hexadecimal.
' Each element is converted to an integer with Convert.ToInt32.
Console.Write("Input string in hexadecimal: ")
Dim c As Char
For Each c In inputString.ToCharArray()
Console.Write("0x{0:X2} ", Convert.ToInt32(c))
Next c
Console.Write(twoNewLines)
' --------------------------------------------------------------------------
' Encode the input string.
Console.WriteLine("Encode the input string...")
numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length, _
encodedBytes, 0)
' Display the encoded bytes.
' Each element is converted to an integer with Convert.ToInt32.
Console.WriteLine("Encoded bytes in hexadecimal ({0} bytes):" & vbCrLf, _
numberOfEncodedBytes)
ix = 0
Dim b As Byte
For Each b In encodedBytes
Console.Write("0x{0:X2} ", Convert.ToInt32(b))
ix += 1
If 0 = ix Mod 6 Then
Console.WriteLine()
End If
Next b
Console.Write(twoNewLines)
' --------------------------------------------------------------------------
' Decode the encoded bytes, yielding a reconstituted string.
Console.WriteLine("Decode the encoded bytes...")
decodedString = ae.GetString(encodedBytes)
' Display the input string and the decoded string for comparison.
Console.WriteLine("Input string: ""{0}""", inputString)
Console.WriteLine("Decoded string:""{0}""", decodedString)
End Sub
End Class
'
'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
'0x29
'
'Decode the encoded bytes...
'Input string: "X"
'Decoded string:"(unknown)X(unknown)"
'
설명
.NET Framework에서 이 메서드는 GetEncoding 기본 플랫폼을 사용하여 대부분의 코드 페이지를 지원합니다. 그러나 .NET Framework는 기본적으로 일부 인코딩을 지원합니다. 코드 페이지 목록은 인코딩 목록을 참조하세요. .NET Core에서 이 메서드는 GetEncoding .NET Core에서 기본적으로 지원되는 인코딩을 반환합니다. 두 .NET 구현 모두에서 메서드를 호출 GetEncodings 하여 사용 가능한 모든 인코딩에 대한 정보를 포함하는 개체 배열 EncodingInfo 을 가져올 수 있습니다.
.NET Core에서 기본적으로 사용할 수 있거나 특정 플랫폼 버전의 .NET Framework GetEncoding 에서 기본적으로 지원되는 인코딩 외에도 이 메서드는 개체를 등록하여 사용할 수 있는 추가 인코딩을 EncodingProvider 반환합니다. 동일한 인코딩이 여러 EncodingProvider 개체에 의해 등록된 경우 이 메서드는 마지막으로 등록된 인코딩을 반환합니다.
.NET 5 이상 버전에서는 코드 페이지 이름이 utf-7 지원되지 않습니다.
메모
ANSI 코드 페이지는 컴퓨터별로 다를 수 있으며 단일 컴퓨터에서 변경되어 데이터가 손상될 수 있습니다. 가장 일관된 결과를 얻으려면 특정 코드 페이지 대신 UTF-8(코드 페이지 65001) 또는 UTF-16과 같은 유니코드 인코딩을 사용해야 합니다.
GetEncoding 는 기본 설정을 사용하여 캐시된 인스턴스를 반환합니다. 파생 클래스의 생성자를 사용하여 다른 설정의 인스턴스를 가져와야 합니다. 예를 들어 클래스는 UTF32Encoding 오류 검색을 사용하도록 설정할 수 있는 생성자를 제공합니다.