|
이 문서는 기계로 번역한 것입니다. 원본 텍스트를 보려면 포인터를 문서의 문장 위로 올리십시오. 추가 정보
|
번역
원본
|
Encoding.GetBytes 메서드 (String)
네임스페이스: System.Text
어셈블리: mscorlib(mscorlib.dll)
매개 변수
- s
- 형식: System.String
인코딩할 문자가 들어 있는 문자열입니다.
| 예외 | 조건 |
|---|---|
| ArgumentNullException | |
| EncoderFallbackException |
응용 프로그램에서 많은 입력 문자를 코드 페이지에 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수도 있습니다. 이 경우 응용 프로그램에서 호출 간에 상태를 유지하고 사용되는 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 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(서버 코어 역할은 지원되지 않음), Windows Server 2008 R2(서버 코어 역할은 SP1 이상에서 지원, Itanium은 지원되지 않음)
.NET Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.