Encoding.GetPreamble 메서드

정의

파생 클래스에서 재정의되면 사용되는 인코딩을 지정하는 바이트 시퀀스를 반환합니다.

public:
 virtual cli::array <System::Byte> ^ GetPreamble();
public virtual byte[] GetPreamble ();
abstract member GetPreamble : unit -> byte[]
override this.GetPreamble : unit -> byte[]
Public Overridable Function GetPreamble () As Byte()

반환

Byte[]

사용되는 인코딩을 지정하는 바이트 시퀀스가 포함된 바이트 배열입니다.

또는

프리앰블이 필요하지 않으면 길이가 0인 바이트 배열입니다.

예제

다음 예에서는 프리앰블에 따라 인코딩의 바이트 순서를 결정 합니다.

using namespace System;
using namespace System::Text;
int main()
{
   Encoding^ unicode = Encoding::Unicode;
   
   // Get the preamble for the Unicode encoder. 
   // In this case the preamblecontains the Byte order mark (BOM).
   array<Byte>^preamble = unicode->GetPreamble();
   
   // Make sure a preamble was returned 
   // and is large enough to containa BOM.
   if ( preamble->Length >= 2 )
   {
      
      // if (preamble->Item[0] == 0xFE && preamble->Item[1] == 0xFF) 
      if ( preamble[ 0 ] == 0xFE && preamble[ 1 ] == 0xFF )
      {
         Console::WriteLine( "The Unicode encoder is encoding in big-endian order." );
      }
      // else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE) 
      else
      
      // else if (preamble->Item[0] == 0xFF && preamble->Item[1] == 0xFE) 
      if ( preamble[ 0 ] == 0xFF && preamble[ 1 ] == 0xFE )
      {
         Console::WriteLine( "The Unicode encoder is encoding in little-endian order." );
      }
   }
}

/*
This code produces the following output.

The Unicode encoder is encoding in little-endian order.

*/
using System;
using System.Text;

namespace GetPreambleExample
{
   class GetPreambleExampleClass
   {
      static void Main()
      {
         Encoding unicode = Encoding.Unicode;

         // Get the preamble for the Unicode encoder. 
         // In this case the preamble contains the byte order mark (BOM).
         byte[] preamble = unicode.GetPreamble();

         // Make sure a preamble was returned 
         // and is large enough to containa BOM.
         if(preamble.Length >= 2)
         {
            if(preamble[0] == 0xFE && preamble[1] == 0xFF)
            {
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.");
            }
            else if(preamble[0] == 0xFF && preamble[1] == 0xFE)
            {
               Console.WriteLine("The Unicode encoder is encoding in little-endian order.");
            }
         }
      }
   }
}

/*
This code produces the following output.

The Unicode encoder is encoding in little-endian order.

*/
Imports System.Text

Namespace GetPreambleExample
   Class GetPreambleExampleClass
      Shared Sub Main()
         Dim [unicode] As Encoding = Encoding.Unicode

         ' Get the preamble for the Unicode encoder. 
         ' In this case the preamble contains the byte order mark (BOM).
         Dim preamble As Byte() = [unicode].GetPreamble()

         ' Make sure a preamble was returned 
         ' and is large enough to contain a BOM.
         If preamble.Length >= 2 Then
            If preamble(0) = &HFE And preamble(1) = &HFF Then
               Console.WriteLine("The Unicode encoder is encoding in big-endian order.")
            Else
               If preamble(0) = &HFF And preamble(1) = &HFE Then
                  Console.WriteLine("The Unicode encoder is encoding in little-endian order.")
               End If
            End If
         End If
      End Sub
   End Class
End Namespace

'This code produces the following output.
'
'The Unicode encoder is encoding in little-endian order.
'

설명

필요에 따라 Encoding 개체는 인코딩 프로세스의 결과로 생성 되는 바이트 시퀀스의 접두사로 사용할 수 있는 바이트 배열인 머리말을 제공 합니다. 프리앰블이 바이트 순서 표시 (유니코드, 코드 포인트 U + FEFF)를 포함 하는 경우 디코더가 바이트 순서와 변환 형식 또는 u t f를 결정 하는 데 도움이 됩니다.

유니코드 바이트 순서 표시 (BOM)는 다음과 같이 serialize 됩니다 (16 진수).

  • UTF-8: EF BB BF

  • UTF-16 big endian 바이트 순서: FE FF

  • UTF-16 little endian 바이트 순서: FF FE

  • U t f-32 big endian 바이트 순서: 00 00 FE FF

  • UTF-32 little endian 바이트 순서: FF FE 00 00

BOM을 사용 해야 합니다 .이 경우에는 개체에 대 한 참조가 손실 된 파일에 대 한 인코딩을 거의 특정 방식으로 식별 하는 것입니다 Encoding . 예를 들어 비즈니스에 국제 관심사 나 기타 데이터가 없는 경우에 저장 된 임의의 텍스트 파일 또는 태그가 지정 된 웹 데이터 또는 임의의 텍스트 파일에 대 한 참조가 손실 됩니다. 데이터가 일관적이 고 적절 하 게 태그가 지정 된 경우 (예를 들어 UTF-8 또는 u t f-16) 사용자 문제를 피할 수 있습니다.

인코딩 유형을 제공 하는 표준의 경우 BOM은 다소 중복 됩니다. 그러나 서버에서 올바른 인코딩 헤더를 보내는 데 사용할 수 있습니다. 또는 인코딩이 손실 되는 경우 대체 방법으로 사용할 수 있습니다.

BOM을 사용 하는 경우 몇 가지 단점이 있습니다. 예를 들어 BOM을 사용 하는 데이터베이스 필드를 제한 하는 방법을 알고 있는 것은 어려울 수 있습니다. 파일의 연결은 예를 들어 불필요 한 문자가 데이터 중간에 종료 될 수 있는 방식으로 파일을 병합 하는 경우에도 문제가 될 수 있습니다. 그러나 몇 가지 단점에도 불구 하 고 BOM을 사용 하는 것이 좋습니다.

바이트 순서 및 바이트 순서 표시에 대 한 자세한 내용은 유니코드 홈페이지에서 유니코드 표준을 참조 하세요.

주의

인코딩된 바이트가 올바르게 디코딩 되도록 하려면 인코딩된 바이트를 프리앰블을 접두사로 사용 해야 합니다. 그러나 대부분의 인코딩은 프리앰블을 제공 하지 않습니다. 인코딩된 바이트가 올바르게 디코딩 되도록 하려면 프리앰블이 포함 된,, 또는 유니코드 인코딩을 사용 해야 합니다 UTF8Encoding UnicodeEncoding UTF32Encoding .

적용 대상