UTF8Encoding 생성자

정의

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다.

오버로드

UTF8Encoding()

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다.

UTF8Encoding(Boolean)

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다. 매개 변수는 유니코드 바이트 순서 표시를 제공할지 여부를 지정합니다.

UTF8Encoding(Boolean, Boolean)

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다. 매개 변수를 사용하여 유니코드 바이트 순서 표시를 제공할지 여부와 잘못된 인코딩이 검색되었을 때 예외를 발생시킬지 여부를 지정할 수 있습니다.

UTF8Encoding()

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다.

public:
 UTF8Encoding();
public UTF8Encoding ();
Public Sub New ()

예제

다음 예제에서는 새 UTF8Encoding 인스턴스를 만들고 해당 이름을 표시합니다.

using namespace System;
using namespace System::Text;
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   String^ encodingName = utf8->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        String encodingName = utf8.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim encodingName As String = utf8.EncodingName
        Console.WriteLine("Encoding name: " & encodingName)
    End Sub
End Class

설명

이 생성자는 유니코드 바이트 순서 표시를 제공하지 않고 잘못된 인코딩이 검색될 때 예외를 throw하지 않는 인스턴스를 만듭니다.

주의

보안상의 이유로 매개 변수를 사용하여 생성자를 throwOnInvalidBytes 호출하고 해당 값을 .로 설정하여 오류 검색을 true사용하도록 설정하는 것이 좋습니다.

추가 정보

적용 대상

UTF8Encoding(Boolean)

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다. 매개 변수는 유니코드 바이트 순서 표시를 제공할지 여부를 지정합니다.

public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier);
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier);
new System.Text.UTF8Encoding : bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean)

매개 변수

encoderShouldEmitUTF8Identifier
Boolean

GetPreamble() 메서드를 통해 유니코드 바이트 순서 표시를 반환하도록 지정하려면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 새 UTF8Encoding 인스턴스를 만들고 메서드에서 유니코드 바이트 순서 표시 접두사 내보내 GetPreamble 기를 지정합니다. 그런 다음, 메서드는 GetPreamble 유니코드 바이트 순서 표시 접두사로 반환합니다.

using namespace System;
using namespace System::Text;
using namespace System::Collections;
void ShowArray( Array^ theArray )
{
   IEnumerator^ myEnum = theArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::Write( "[{0}]", o );
   }

   Console::WriteLine();
}

int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8EmitBOM = gcnew UTF8Encoding( true );
   Console::WriteLine( "utf8 preamble:" );
   ShowArray( utf8->GetPreamble() );
   Console::WriteLine( "utf8EmitBOM:" );
   ShowArray( utf8EmitBOM->GetPreamble() );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8EmitBOM = new UTF8Encoding(true);

        Console.WriteLine("utf8 preamble:");
        ShowArray(utf8.GetPreamble());

        Console.WriteLine("utf8EmitBOM:");
        ShowArray(utf8EmitBOM.GetPreamble());
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8EmitBOM As New UTF8Encoding(True)
        
        Console.WriteLine("utf8 preamble:")
        ShowArray(utf8.GetPreamble())
        
        Console.WriteLine("utf8EmitBOM:")
        ShowArray(utf8EmitBOM.GetPreamble())
    End Sub
    
    
    Public Shared Sub ShowArray(theArray As Array)
        Dim o As Object
        For Each o In  theArray
            Console.Write("[{0}]", o)
        Next o
        Console.WriteLine()
    End Sub
End Class

설명

이 생성자는 잘못된 인코딩이 검색될 때 예외를 throw하지 않는 인스턴스를 만듭니다.

주의

보안상의 이유로 매개 변수가 포함된 throwOnInvalidBytes 생성자를 호출하고 해당 값을 .로 설정하여 오류 검색을 true사용하도록 설정해야 합니다.

매개 변수는 encoderShouldEmitUTF8Identifier 메서드의 작업을 제어합니다 GetPreamble . 이 경우 true메서드는 UTF-8 형식으로 BOM(유니코드 바이트 순서 표시)이 포함된 바이트 배열을 반환합니다. 이 경우 false길이가 0인 바이트 배열을 반환합니다. 그러나 설정 encoderShouldEmitUTF8Identifier true 은 메서드가 GetBytes 바이트 배열의 시작 부분에 BOM을 접두사로 지정하지 않으며 메서드가 BOM의 바이트 수를 바이트 수에 포함시키지 않습니다 GetByteCount .

추가 정보

적용 대상

UTF8Encoding(Boolean, Boolean)

UTF8Encoding 클래스의 새 인스턴스를 초기화합니다. 매개 변수를 사용하여 유니코드 바이트 순서 표시를 제공할지 여부와 잘못된 인코딩이 검색되었을 때 예외를 발생시킬지 여부를 지정할 수 있습니다.

public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
new System.Text.UTF8Encoding : bool * bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean, throwOnInvalidBytes As Boolean)

매개 변수

encoderShouldEmitUTF8Identifier
Boolean

GetPreamble() 메서드를 통해 유니코드 바이트 순서 표시를 반환하도록 지정하려면 true이고, 그렇지 않으면 false입니다.

throwOnInvalidBytes
Boolean

잘못된 인코딩이 검색되었을 때 예외를 throw하려면 true이고, 그러지 않으면 false입니다.

예제

다음 예제에서는 메서드가 GetPreamble 유니코드 바이트 순서 표시 접두사로 내보내지 않도록 지정하는 새 UTF8Encoding 인스턴스를 만들고 잘못된 인코딩이 검색되면 예외를 throw해야 합니다. 이 생성자의 동작은 잘못된 인코딩이 검색될 때 예외를 throw하지 않는 기본 UTF8Encoding() 생성자와 비교됩니다. 두 UTF8Encoding 인스턴스는 두 개의 높은 서로게이트(U+D801 및 U+D802)가 행에 포함된 문자 배열을 인코딩합니다. 이는 잘못된 문자 시퀀스입니다. 상위 서로게이트 뒤에는 항상 낮은 서로게이트가 와야 합니다.

using namespace System;
using namespace System::Text;

void ShowArray(Array^ theArray)
{
   for each (Byte b in theArray) {
      Console::Write( "{0:X2} ", b);
   }
   Console::WriteLine();
}

int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8ThrowException = gcnew UTF8Encoding(false,true);
   
   // This array contains two high surrogates in a row (\uD801, \uD802).
   array<Char>^chars = {'a','b','c',L'\xD801',L'\xD802','d'};
   
   // The following method call will not throw an exception.
   array<Byte>^bytes = utf8->GetBytes( chars );
   ShowArray( bytes );
   Console::WriteLine();
   
   try {
      
      // The following method call will throw an exception.
      bytes = utf8ThrowException->GetBytes( chars );
   }
   catch (EncoderFallbackException^ e ) {
            Console::WriteLine("{0} exception\nMessage:\n{1}",
                               e->GetType()->Name, e->Message);
   }

}
using System;
using System.Text;

class Example
{
    public static void Main()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);

        // Create an array with two high surrogates in a row (\uD801, \uD802).
        Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'};

        // The following method call will not throw an exception.
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);
        Console.WriteLine();

        try {
            // The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars);
            ShowArray(bytes);
        }
        catch (EncoderFallbackException e) {
            Console.WriteLine("{0} exception\nMessage:\n{1}",
                              e.GetType().Name, e.Message);
        }
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray)
            Console.Write("{0:X2} ", o);

        Console.WriteLine();
    }
}
// The example displays the following output:
//    61 62 63 EF BF BD EF BF BD 64
//
//    EncoderFallbackException exception
//    Message:
//    Unable to translate Unicode character \uD801 at index 3 to specified code page.
Imports System.Text

Class Example
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8ThrowException As New UTF8Encoding(False, True)
        
        ' Create an array with two high surrogates in a row (\uD801, \uD802).
        Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(&hD801), ChrW(&hD802), "d"c}
        
        ' The following method call will not throw an exception.
        Dim bytes As Byte() = utf8.GetBytes(chars)
        ShowArray(bytes)
        Console.WriteLine()
        
        Try
            ' The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars)
            ShowArray(bytes)
        Catch e As EncoderFallbackException
            Console.WriteLine("{0} exception{2}Message:{2}{1}",
                              e.GetType().Name, e.Message, vbCrLf)
        End Try
    End Sub
    
    
    Public Shared Sub ShowArray(theArray As Array)
        For Each o In theArray
            Console.Write("{0:X2} ", o)
        Next
        Console.WriteLine()
    End Sub
End Class
' The example displays the following output:
'    61 62 63 EF BF BD EF BF BD 64
'
'    EncoderFallbackException exception
'    Message:
'    Unable to translate Unicode character \uD801 at index 3 to specified code page.

설명

매개 변수는 encoderShouldEmitUTF8Identifier 메서드의 작업을 제어합니다 GetPreamble . 이 경우 true메서드는 UTF-8 형식으로 BOM(유니코드 바이트 순서 표시)이 포함된 바이트 배열을 반환합니다. 이 경우 false길이가 0인 바이트 배열을 반환합니다. 그러나 설정 encoderShouldEmitUTF8Identifier true 은 메서드가 GetBytes 바이트 배열의 시작 부분에 BOM을 접두사로 지정하지 않으며 메서드가 BOM의 바이트 수를 바이트 수에 포함시키지 않습니다 GetByteCount .

true경우 throwOnInvalidBytes 잘못된 바이트 시퀀스를 검색하는 메서드가 예외를 System.ArgumentException throw합니다. 그렇지 않으면 메서드가 예외를 throw하지 않고 잘못된 시퀀스가 무시됩니다.

주의

보안상의 이유로 매개 변수가 포함된 throwOnInvalidBytes 생성자를 호출하고 해당 매개 변수를 .로 설정하여 오류 검색을 true사용하도록 설정해야 합니다.

추가 정보

적용 대상