Encoding.UTF8 속성

정의

UTF-8 형식에 대한 인코딩을 가져옵니다.

public:
 static property System::Text::Encoding ^ UTF8 { System::Text::Encoding ^ get(); };
public static System.Text.Encoding UTF8 { get; }
member this.UTF8 : System.Text.Encoding
Public Shared ReadOnly Property UTF8 As Encoding

속성 값

Encoding

UTF-8 형식에 대한 인코딩입니다.

예제

다음 예제에서는 다음 문자로 구성 된 배열을 정의 합니다.

  • 라틴어 작은 LETTER Z (U + 007A)

  • 라틴 소문자 A (U + 0061)

  • 결합 리브 (U + 0306)

  • 라틴어 SMALL LETTER AE (U + 01FD)

  • 그리스어 소문자 베타 (U + 03B2)

  • 그리스어 ACROPHONIC ATTIC 1000 STERS (U + 10154)를 형성 하는 서로게이트 쌍 (U + D 800 U + DD54)입니다.

각 문자의 UTF-16 코드 단위를 표시 하 고 UTF-8 인코더에서 문자 배열을 인코딩하기 위해 필요한 바이트 수를 결정 합니다. 그런 다음 문자를 인코딩하고 결과 u t f-8로 인코딩된 바이트를 표시 합니다.

using System;
using System.Text;

public class Example
{
   public static void Main()  
   {
      // Create a character array.
      string gkNumber = Char.ConvertFromUtf32(0x10154);
      char[] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', 
                                  gkNumber[0], gkNumber[1] };

      // Get UTF-8 and UTF-16 encoders.
      Encoding utf8 = Encoding.UTF8;
      Encoding utf16 = Encoding.Unicode;
      
      // Display the original characters' code units.
      Console.WriteLine("Original UTF-16 code units:");
      byte[] utf16Bytes = utf16.GetBytes(chars);
      foreach (var utf16Byte in utf16Bytes)
         Console.Write("{0:X2} ", utf16Byte);
      Console.WriteLine();
         
      // Display the number of bytes required to encode the array.
      int reqBytes  = utf8.GetByteCount(chars);
      Console.WriteLine("\nExact number of bytes required: {0}", 
                    reqBytes);

      // Display the maximum byte count.
      int maxBytes = utf8.GetMaxByteCount(chars.Length);
      Console.WriteLine("Maximum number of bytes required: {0}\n", 
                        maxBytes);

      // Encode the array of chars.
      byte[] utf8Bytes = utf8.GetBytes(chars);

      // Display all the UTF-8-encoded bytes.
      Console.WriteLine("UTF-8-encoded code units:");
      foreach (var utf8Byte in utf8Bytes)
         Console.Write("{0:X2} ", utf8Byte);
      Console.WriteLine();
   }
}
// The example displays the following output:
//       Original UTF-16 code units:
//       7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD
//       
//       Exact number of bytes required: 12
//       Maximum number of bytes required: 24
//       
//       UTF-8-encoded code units:
//       7A 61 CC 86 C7 BD CE B2 F0 90 85 94
Imports System.Text

Public Module Example
   Public Sub Main()
      ' Create a character array.
      Dim gkNumber As String = Char.ConvertFromUtf32(&h10154)
      Dim chars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), 
                             ChrW(&H03B2), gkNumber(0), gkNumber(1) }
 
      ' Get UTF-8 and UTF-16 encoders.
      Dim utf8 As Encoding = Encoding.UTF8
      Dim utf16 As Encoding = Encoding.Unicode

      ' Display the original characters' code units.
      Console.WriteLine("Original UTF-16 code units:")
      Dim utf16Bytes() As Byte = utf16.GetBytes(chars)
      For Each utf16Byte In utf16Bytes
         Console.Write("{0:X2} ", utf16Byte)
      Next
      Console.WriteLine()

      Console.WriteLine()
      ' Display the number of bytes required to encode the array.
      Dim reqBytes As Integer = utf8.GetByteCount(chars)
      Console.WriteLine("Exact number of bytes required: {0}", 
                        reqBytes)

      ' Display the maximum byte count.
      Dim maxBytes As Integer = utf8.GetMaxByteCount(chars.Length)
      Console.WriteLine("Maximum number of bytes required: {0}", 
                        maxBytes)
      Console.WriteLine()
      
      ' Encode the array of characters.
      Dim utf8Bytes() As Byte = utf8.GetBytes(chars)

      ' Display all the UTF-8-encoded bytes.
      Console.WriteLine("UTF-8-encoded code units:")
      For Each utf8Byte In utf8Bytes
         Console.Write("{0:X2} ", utf8Byte)
      Next
      Console.WriteLine()
   End Sub 
End Module 
' The example displays the following output:
'    Original UTF-16 code units:
'    7A 00 61 00 06 03 FD 01 B2 03 00 D8 54 DD
'    
'    Exact number of bytes required: 12
'    Maximum number of bytes required: 24
'    
'    UTF-8-encoded code units:
'    7A 61 CC 86 C7 BD CE B2 F0 90 85 94

설명

이 속성은 UTF8Encoding 유니코드 (utf-16 인코딩) 문자를 문자 당 1 ~ 4 바이트 시퀀스로 인코딩하고, u t f-8로 인코딩된 바이트 배열을 유니코드 (utf-16 인코딩) 문자로 디코딩하는 개체를 반환 합니다. .NET에서 지원 되는 문자 인코딩과 사용할 유니코드 인코딩에 대 한 자세한 내용은 .net의 문자 인코딩을 참조 하세요.

UTF8Encoding이 속성에서 반환 되는 개체에는 앱에 대 한 적절 한 동작이 없을 수 있습니다.

  • 이 클래스는 UTF8Encoding 유니코드 BOM (바이트 순서 표시)을 제공 하는 개체를 반환 합니다. BOM을 제공 하지 않는 UTF8 인코딩을 인스턴스화하려면 생성자의 오버 로드를 호출 UTF8Encoding 합니다.

  • 이 메서드는 UTF8Encoding 대체 대체 (fallback)를 사용 하 여 인코딩할 수 없는 각 문자열과 물음표 ("?") 문자로 디코딩할 수 없는 각 바이트를 대체 하는 개체를 반환 합니다. 대신 UTF8Encoding.UTF8Encoding(Boolean, Boolean) UTF8Encoding EncoderFallbackException DecoderFallbackException 다음 예제에서 보여 주는 것 처럼 대체 (fallback)가 있는 개체를 인스턴스화하기 위해 생성자를 호출할 수 있습니다.

    using System;
    using System.Text;
    
    public class Example
    {
       public static void Main()
       {
          Encoding enc = new UTF8Encoding(true, true);
          string value = "\u00C4 \uD802\u0033 \u00AE"; 
    
          try {
             byte[] bytes= enc.GetBytes(value);
             foreach (var byt in bytes)
                Console.Write("{0:X2} ", byt);
             Console.WriteLine();
    
             string value2 = enc.GetString(bytes);
             Console.WriteLine(value2);
          }
          catch (EncoderFallbackException e) {
             Console.WriteLine("Unable to encode {0} at index {1}", 
                               e.IsUnknownSurrogate() ? 
                                  String.Format("U+{0:X4} U+{1:X4}", 
                                                Convert.ToUInt16(e.CharUnknownHigh),
                                                Convert.ToUInt16(e.CharUnknownLow)) :
                                  String.Format("U+{0:X4}", 
                                                Convert.ToUInt16(e.CharUnknown)),
                               e.Index);
          }                     
       }
    }
    // The example displays the following output:
    //        Unable to encode U+D802 at index 2
    
    Imports System.Text
    
    Module Example
       Public Sub Main()
          Dim enc As Encoding = New UTF8Encoding(True, True)
          Dim value As String = String.Format("{0} {1}{2} {3}", 
                                ChrW(&h00C4), ChrW(&hD802), ChrW(&h0033), ChrW(&h00AE))
          
          Try
             Dim bytes() As Byte = enc.GetBytes(value)
             For Each byt As Byte In bytes
                Console.Write("{0:X2} ", byt)
             Next       
             Console.WriteLine()
             Dim value2 As String = enc.GetString(bytes)
             Console.WriteLine(value2)
          Catch e As EncoderFallbackException
             Console.WriteLine("Unable to encode {0} at index {1}", 
                               If(e.IsUnknownSurrogate(), 
                                  String.Format("U+{0:X4} U+{1:X4}", 
                                                Convert.ToUInt16(e.CharUnknownHigh),
                                                Convert.ToUInt16(e.CharUnknownLow)),
                                  String.Format("U+{0:X4}", 
                                                Convert.ToUInt16(e.CharUnknown))),
                               e.Index)
          End Try
       End Sub
    End Module
    ' The example displays the following output:
    '       Unable to encode U+D802 at index 2
    

적용 대상

추가 정보