이 문서는 기계로 번역한 것입니다. 원본 텍스트를 보려면 포인터를 문서의 문장 위로 올리십시오. 추가 정보
번역
원본
이 항목은 아직 평가되지 않았습니다.- 이 항목 평가

Encoding.GetBytes 메서드 (String)

파생 클래스에서 재정의될 때 지정한 문자열의 모든 문자를 바이트 시퀀스로 인코딩합니다.

네임스페이스:  System.Text
어셈블리:  mscorlib(mscorlib.dll)
public virtual byte[] GetBytes(
	string s
)

매개 변수

s
형식: System.String
인코딩할 문자가 들어 있는 문자열입니다.

반환 값

형식: System.Byte[]
지정한 문자 집합을 인코딩한 결과가 들어 있는 바이트 배열입니다.
예외조건
ArgumentNullException

snull입니다.

EncoderFallbackException

대체(fallback)가 발생한 경우. 자세한 내용은 .NET Framework의 문자 인코딩를 참조하십시오.

-및-

EncoderFallback EncoderExceptionFallback로 설정됩니다.

변환할 데이터를 순차 블록에서만 사용할 수 있거나(예: 스트림에서 읽은 데이터) 데이터의 양이 너무 커서 여러 개의 작은 블록으로 나눠야 하는 경우 응용 프로그램에서는 파생 클래스의 GetDecoder 메서드나 GetEncoder 메서드에서 각기 제공하는 Decoder 또는 Encoder를 사용해야 합니다.

GetByteCount 메서드는 유니코드 문자 집합을 인코딩한 결과의 바이트 수를 결정하며 GetBytes 메서드는 실제 인코딩을 수행합니다. Encoding.GetBytes 메서드는 단일 입력 스트림에서 여러 변환을 처리하는 Encoder.GetBytes 메서드와 달리 분리된 변환이 필요합니다.

GetByteCount GetBytes의 여러 버전이 지원됩니다. 다음은 이 메서드 사용과 관련된 몇 가지 프로그래밍 고려 사항입니다.

  • 응용 프로그램에서 많은 입력 문자를 코드 페이지에 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수도 있습니다. 이 경우 응용 프로그램에서 호출 간에 상태를 유지하고 사용되는 Encoder 개체에 의해 보관된 상태를 고려해야 합니다. 예를 들어, 서로게이트 쌍을 포함하는 문자 시퀀스는 상위 서로게이트 끝날 수도 있습니다. Encoder 는 그 high surrogate를 기억하여 이것이 다음 호출 시작시에 low surrogate와 결합되도록 할 것입니다. Encoding 은 그 상태를 지속할 수 없을 것이기 때문에, 그 문자는 EncoderFallback으로 전달됩니다.)

  • 응용 프로그램에서 문자열 입력을 처리하는 경우 GetBytes의 문자열 버전을 사용하는 것이 좋습니다.

  • GetBytes(Char*, Int32, Byte*, Int32) 의 유니코드 문자 버퍼 버전에서는 특히 Encoder 개체를 사용하거나 기존 버퍼에 삽입하는 여러 호출과 관련해서 몇 가지 빠른 방법을 사용할 수 있습니다. 그러나 이 메서드 버전은 포인터가 필요하므로 안전하지 않을 수 있습니다.

  • 응용 프로그램에서 많은 양의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 바이트 배열을 지원하는 GetBytes 버전이 가장 적합합니다.

  • GetByteCount 대신 Encoder.Convert 메서드를 사용합니다. 이 변환 메서드는 가능한 한 많은 데이터를 변환하며 출력 버퍼가 너무 작은 경우 예외를 throw합니다. 스트림의 연속 인코딩에는 이 메서드가 가장 적합할 수 있습니다.

다음 코드 예제에서는 문자열 또는 문자열 범위를 인코딩하는 데 필요한 바이트 수를 확인하고 문자를 인코딩한 다음 결과 바이트를 표시합니다.


using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // The characters to encode:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      //    a high-surrogate value (U+D8FF)
      //    a low-surrogate value (U+DCFF)
      String myStr = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";

      // Get different encodings.
      Encoding  u7    = Encoding.UTF7;
      Encoding  u8    = Encoding.UTF8;
      Encoding  u16LE = Encoding.Unicode;
      Encoding  u16BE = Encoding.BigEndianUnicode;
      Encoding  u32   = Encoding.UTF32;

      // Encode the entire string, and print out the counts and the resulting bytes.
      Console.WriteLine( "Encoding the entire string:" );
      PrintCountsAndBytes( myStr, u7 );
      PrintCountsAndBytes( myStr, u8 );
      PrintCountsAndBytes( myStr, u16LE );
      PrintCountsAndBytes( myStr, u16BE );
      PrintCountsAndBytes( myStr, u32 );

      Console.WriteLine();

      // Encode three characters starting at index 4, and print out the counts and the resulting bytes.
      Console.WriteLine( "Encoding the characters from index 4 through 6:" );
      PrintCountsAndBytes( myStr, 4, 3, u7 );
      PrintCountsAndBytes( myStr, 4, 3, u8 );
      PrintCountsAndBytes( myStr, 4, 3, u16LE );
      PrintCountsAndBytes( myStr, 4, 3, u16BE );
      PrintCountsAndBytes( myStr, 4, 3, u32 );

   }


   public static void PrintCountsAndBytes( String s, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-30} :", enc.ToString() );

      // Display the exact byte count.
      int iBC  = enc.GetByteCount( s );
      Console.Write( " {0,-3}", iBC );

      // Display the maximum byte count.
      int iMBC = enc.GetMaxByteCount( s.Length );
      Console.Write( " {0,-3} :", iMBC );

      // Encode the entire string.
      byte[] bytes = enc.GetBytes( s );

      // Display all the encoded bytes.
      PrintHexBytes( bytes );

   }


   public static void PrintCountsAndBytes( String s, int index, int count, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-30} :", enc.ToString() );

      // Display the exact byte count.
      int iBC  = enc.GetByteCount( s.ToCharArray(), index, count );
      Console.Write( " {0,-3}", iBC );

      // Display the maximum byte count.
      int iMBC = enc.GetMaxByteCount( count );
      Console.Write( " {0,-3} :", iMBC );

      // Encode a range of characters in the string.
      byte[] bytes = new byte[iBC];
      enc.GetBytes( s, index, count, bytes, bytes.GetLowerBound(0) );

      // Display all the encoded bytes.
      PrintHexBytes( bytes );

   }


   public static void PrintHexBytes( byte[] bytes )  {

      if (( bytes == null ) || ( bytes.Length == 0 ))
         Console.WriteLine( "<none>" );
      else  {
         for ( int i = 0; i < bytes.Length; i++ )
            Console.Write( "{0:X2} ", bytes[i] );
         Console.WriteLine();
      }

   }

}


/* 
This code produces the following output.

Encoding the entire string:
System.Text.UTF7Encoding       : 18  23  :7A 61 2B 41 77 59 42 2F 51 4F 79 32 50 2F 63 2F 77 2D
System.Text.UTF8Encoding       : 12  24  :7A 61 CC 86 C7 BD CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding    : 14  16  :7A 00 61 00 06 03 FD 01 B2 03 FF D8 FF DC
System.Text.UnicodeEncoding    : 14  16  :00 7A 00 61 03 06 01 FD 03 B2 D8 FF DC FF
System.Text.UTF32Encoding      : 24  32  :7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00 FF FC 04 00

Encoding the characters from index 4 through 6:
System.Text.UTF7Encoding       : 10  11  :2B 41 37 4C 59 2F 39 7A 2F 2D
System.Text.UTF8Encoding       : 6   12  :CE B2 F1 8F B3 BF
System.Text.UnicodeEncoding    : 6   8   :B2 03 FF D8 FF DC
System.Text.UnicodeEncoding    : 6   8   :03 B2 D8 FF DC FF
System.Text.UTF32Encoding      : 8   16  :B2 03 00 00 FF FC 04 00

*/



.NET Framework

4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0에서 지원

.NET Framework Client Profile

4, 3.5 SP1에서 지원

이식 가능한 클래스 라이브러리

이식 가능한 클래스 라이브러리에서 지원

Windows 스토어 앱용 .NET

Windows 8에서 지원

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(서버 코어 역할은 지원되지 않음), Windows Server 2008 R2(서버 코어 역할은 SP1 이상에서 지원, Itanium은 지원되지 않음)

.NET Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.
이 정보가 도움이 되었습니까?
(1500자 남음)

커뮤니티 추가 항목

추가
© 2013 Microsoft. All rights reserved.