Encoding.GetBytes 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
파생 클래스에서 재정의되는 경우 문자 집합을 바이트 시퀀스로 인코딩합니다.
오버로드
| Name | Description |
|---|---|
| GetBytes(Char[]) |
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 모든 문자를 바이트 시퀀스로 인코딩합니다. |
| GetBytes(String) |
파생 클래스에서 재정의되는 경우 지정된 문자열의 모든 문자를 바이트 시퀀스로 인코딩합니다. |
| GetBytes(ReadOnlySpan<Char>, Span<Byte>) |
파생 클래스에서 재정의되는 경우 지정된 읽기 전용 범위의 문자 집합을 바이트 범위로 인코딩합니다. |
| GetBytes(Char[], Int32, Int32) |
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합을 바이트 시퀀스로 인코딩합니다. |
| GetBytes(String, Int32, Int32) |
파생 클래스에서 재정의되는 경우 지정된 문자열에서 지정된 |
| GetBytes(Char*, Int32, Byte*, Int32) |
파생 클래스에서 재정의되는 경우 지정된 문자 포인터에서 시작하는 문자 집합을 지정된 바이트 포인터에서 시작하여 저장되는 바이트 시퀀스로 인코딩합니다. |
| GetBytes(Char[], Int32, Int32, Byte[], Int32) |
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합을 지정된 바이트 배열로 인코딩합니다. |
| GetBytes(String, Int32, Int32, Byte[], Int32) |
파생 클래스에서 재정의되는 경우 지정된 문자열의 문자 집합을 지정된 바이트 배열로 인코딩합니다. |
GetBytes(Char[])
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 모든 문자를 바이트 시퀀스로 인코딩합니다.
public:
virtual cli::array <System::Byte> ^ GetBytes(cli::array <char> ^ chars);
public virtual byte[] GetBytes(char[] chars);
abstract member GetBytes : char[] -> byte[]
override this.GetBytes : char[] -> byte[]
Public Overridable Function GetBytes (chars As Char()) As Byte()
매개 변수
- chars
- Char[]
인코딩할 문자를 포함하는 문자 배열입니다.
반품
지정된 문자 집합을 인코딩한 결과를 포함하는 바이트 배열입니다.
예외
chars은 null입니다.
예제
다음 예제에서는 문자 배열을 인코딩하는 데 필요한 바이트 수를 결정하고 문자를 인코딩하고 결과 바이트를 표시합니다.
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)
char[] myChars = new char[] { 'z', 'a', '\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 array, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, u7 );
PrintCountsAndBytes( myChars, u8 );
PrintCountsAndBytes( myChars, u16LE );
PrintCountsAndBytes( myChars, u16BE );
PrintCountsAndBytes( myChars, u32 );
}
public static void PrintCountsAndBytes( char[] chars, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-30} :", enc.ToString() );
// Display the exact byte count.
int iBC = enc.GetByteCount( chars );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( chars.Length );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars );
// 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.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF)}
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode the entire array, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, u7)
PrintCountsAndBytes(myChars, u8)
PrintCountsAndBytes(myChars, u16LE)
PrintCountsAndBytes(myChars, u16BE)
PrintCountsAndBytes(myChars, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(chars.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'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
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 메서드의 GetBytes 문자열 버전을 호출해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
추가 정보
적용 대상
GetBytes(String)
파생 클래스에서 재정의되는 경우 지정된 문자열의 모든 문자를 바이트 시퀀스로 인코딩합니다.
public:
virtual cli::array <System::Byte> ^ GetBytes(System::String ^ s);
public virtual byte[] GetBytes(string s);
abstract member GetBytes : string -> byte[]
override this.GetBytes : string -> byte[]
Public Overridable Function GetBytes (s As String) As Byte()
매개 변수
- s
- String
인코딩할 문자가 포함된 문자열입니다.
반품
지정된 문자 집합을 인코딩한 결과를 포함하는 바이트 배열입니다.
예외
s은 null입니다.
예제
다음 예제에서는 문자열 또는 문자열의 범위를 인코딩하는 데 필요한 바이트 수를 결정하고 문자를 인코딩하고 결과 바이트를 표시합니다.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = 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)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'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
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
추가 정보
적용 대상
GetBytes(ReadOnlySpan<Char>, Span<Byte>)
파생 클래스에서 재정의되는 경우 지정된 읽기 전용 범위의 문자 집합을 바이트 범위로 인코딩합니다.
public:
virtual int GetBytes(ReadOnlySpan<char> chars, Span<System::Byte> bytes);
public virtual int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes);
abstract member GetBytes : ReadOnlySpan<char> * Span<byte> -> int
override this.GetBytes : ReadOnlySpan<char> * Span<byte> -> int
Public Overridable Function GetBytes (chars As ReadOnlySpan(Of Char), bytes As Span(Of Byte)) As Integer
매개 변수
- chars
- ReadOnlySpan<Char>
인코딩할 문자 집합이 포함된 범위입니다.
반품
인코딩된 바이트 수입니다.
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
적용 대상
GetBytes(Char[], Int32, Int32)
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합을 바이트 시퀀스로 인코딩합니다.
public:
virtual cli::array <System::Byte> ^ GetBytes(cli::array <char> ^ chars, int index, int count);
public virtual byte[] GetBytes(char[] chars, int index, int count);
abstract member GetBytes : char[] * int * int -> byte[]
override this.GetBytes : char[] * int * int -> byte[]
Public Overridable Function GetBytes (chars As Char(), index As Integer, count As Integer) As Byte()
매개 변수
- chars
- Char[]
인코딩할 문자 집합이 포함된 문자 배열입니다.
- index
- Int32
인코딩할 첫 번째 문자의 인덱스입니다.
- count
- Int32
인코딩할 문자 수입니다.
반품
지정된 문자 집합을 인코딩한 결과를 포함하는 바이트 배열입니다.
예외
chars은 null입니다.
예제
다음 예제에서는 문자 배열에서 세 문자를 인코딩하고 문자를 인코딩하고 결과 바이트를 표시하는 데 필요한 바이트 수를 결정합니다.
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)
char[] myChars = new char[] { 'z', 'a', '\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 three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, 4, 3, u7 );
PrintCountsAndBytes( myChars, 4, 3, u8 );
PrintCountsAndBytes( myChars, 4, 3, u16LE );
PrintCountsAndBytes( myChars, 4, 3, u16BE );
PrintCountsAndBytes( myChars, 4, 3, u32 );
}
public static void PrintCountsAndBytes( char[] chars, 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( chars, index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars, index, count );
// The following is an alternative way to encode the array of chars:
// byte[] bytes = new byte[iBC];
// enc.GetBytes( chars, 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.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF) }
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, 4, 3, u7)
PrintCountsAndBytes(myChars, 4, 3, u8)
PrintCountsAndBytes(myChars, 4, 3, u16LE)
PrintCountsAndBytes(myChars, 4, 3, u16BE)
PrintCountsAndBytes(myChars, 4, 3, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars, index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars, index, count)
' The following is an alternative way to encode the array of chars:
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
' Dim bytes(iBC - 1) As Byte
' enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) )
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'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
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
추가 정보
적용 대상
GetBytes(String, Int32, Int32)
파생 클래스에서 재정의되는 경우 지정된 문자열에서 지정된 count 문자 수부터 시작하여 바이트 배열로 인코딩합니다 index.
public:
cli::array <System::Byte> ^ GetBytes(System::String ^ s, int index, int count);
public byte[] GetBytes(string s, int index, int count);
member this.GetBytes : string * int * int -> byte[]
Public Function GetBytes (s As String, index As Integer, count As Integer) As Byte()
매개 변수
- s
- String
인코딩할 문자가 포함된 문자열입니다.
- index
- Int32
인코딩을 시작할 문자열 내의 인덱스입니다.
- count
- Int32
인코딩할 문자 수입니다.
반품
지정된 문자 집합을 인코딩한 결과를 포함하는 바이트 배열입니다.
예제
다음 예제에서는 문자열 또는 문자열의 범위를 인코딩하는 데 필요한 바이트 수를 결정하고 문자를 인코딩하고 결과 바이트를 표시합니다.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = 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)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'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
설명
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
적용 대상
GetBytes(Char*, Int32, Byte*, Int32)
Important
이 API는 CLS 규격이 아닙니다.
파생 클래스에서 재정의되는 경우 지정된 문자 포인터에서 시작하는 문자 집합을 지정된 바이트 포인터에서 시작하여 저장되는 바이트 시퀀스로 인코딩합니다.
public:
virtual int GetBytes(char* chars, int charCount, System::Byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[System.CLSCompliant(false)]
public virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
[<System.CLSCompliant(false)>]
abstract member GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
override this.GetBytes : nativeptr<char> * int * nativeptr<byte> * int -> int
매개 변수
- chars
- Char*
인코딩할 첫 번째 문자에 대한 포인터입니다.
- charCount
- Int32
인코딩할 문자 수입니다.
- bytes
- Byte*
결과 바이트 시퀀스 쓰기를 시작할 위치에 대한 포인터입니다.
- byteCount
- Int32
쓸 최대 바이트 수입니다.
반품
매개 변수가 나타내는 위치에 기록된 실제 바이트 수 bytes 입니다.
- 특성
예외
charCount 또는 byteCount 0보다 작습니다.
byteCount 는 결과 바이트 수보다 작습니다.
설명
결과 바이트를 저장해야 하는 GetBytes 정확한 배열 크기를 계산하려면 메서드를 호출합니다 GetByteCount . 최대 배열 크기를 계산하려면 메서드를 호출합니다 GetMaxByteCount . 이 메서드는 GetByteCount 일반적으로 메모리를 더 적게 할당할 수 있지만 메서드는 GetMaxByteCount 일반적으로 더 빠르게 실행됩니다.
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나 데이터의 양이 너무 커서 더 작은 블록으로 나누어야 하는 경우 파생 클래스의 개체 또는 Encoder 메서드에서 제공하는 GetDecoder 개체를 GetEncoder 각각 사용해야 Decoder 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
추가 정보
적용 대상
GetBytes(Char[], Int32, Int32, Byte[], Int32)
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합을 지정된 바이트 배열로 인코딩합니다.
public:
abstract int GetBytes(cli::array <char> ^ chars, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex);
abstract member GetBytes : char[] * int * int * byte[] * int -> int
Public MustOverride Function GetBytes (chars As Char(), charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer
매개 변수
- chars
- Char[]
인코딩할 문자 집합이 포함된 문자 배열입니다.
- charIndex
- Int32
인코딩할 첫 번째 문자의 인덱스입니다.
- charCount
- Int32
인코딩할 문자 수입니다.
- bytes
- Byte[]
결과 바이트 시퀀스를 포함할 바이트 배열입니다.
- byteIndex
- Int32
결과 바이트 시퀀스 작성을 시작할 인덱스입니다.
반품
에 기록된 실제 바이트 수입니다 bytes.
예외
charIndex 또는 charCountbyteIndex 0보다 작습니다.
-또는-
charIndex 에서 charCount 유효한 범위를 chars나타내지 않습니다.
-또는-
byteIndex에서 유효한 인덱스가 아닌 경우 bytes
bytes 에는 결과 바이트를 수용할 수 있는 충분한 용량 byteIndex 이 없습니다.
예제
다음 예제에서는 문자 배열에서 세 문자를 인코딩하고 문자를 인코딩하고 결과 바이트를 표시하는 데 필요한 바이트 수를 결정합니다.
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)
char[] myChars = new char[] { 'z', 'a', '\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 three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes( myChars, 4, 3, u7 );
PrintCountsAndBytes( myChars, 4, 3, u8 );
PrintCountsAndBytes( myChars, 4, 3, u16LE );
PrintCountsAndBytes( myChars, 4, 3, u16BE );
PrintCountsAndBytes( myChars, 4, 3, u32 );
}
public static void PrintCountsAndBytes( char[] chars, 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( chars, index, count );
Console.Write( " {0,-3}", iBC );
// Display the maximum byte count.
int iMBC = enc.GetMaxByteCount( count );
Console.Write( " {0,-3} :", iMBC );
// Encode the array of chars.
byte[] bytes = enc.GetBytes( chars, index, count );
// The following is an alternative way to encode the array of chars:
// byte[] bytes = new byte[iBC];
// enc.GetBytes( chars, 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.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF) }
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = Encoding.UTF32
' Encode three characters starting at index 4, and print out the counts and the resulting bytes.
PrintCountsAndBytes(myChars, 4, 3, u7)
PrintCountsAndBytes(myChars, 4, 3, u8)
PrintCountsAndBytes(myChars, 4, 3, u16LE)
PrintCountsAndBytes(myChars, 4, 3, u16BE)
PrintCountsAndBytes(myChars, 4, 3, u32)
End Sub
Public Shared Sub PrintCountsAndBytes(chars() As Char, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(chars, index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode the array of chars.
Dim bytes As Byte() = enc.GetBytes(chars, index, count)
' The following is an alternative way to encode the array of chars:
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
' Dim bytes(iBC - 1) As Byte
' enc.GetBytes( chars, index, count, bytes, bytes.GetLowerBound(0) )
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'This code produces the following output.
'
'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
설명
결과 바이트를 저장하는 데 필요한 GetBytes 정확한 배열 크기를 계산하려면 메서드를 GetByteCount 호출해야 합니다. 최대 배열 크기를 계산하려면 메서드를 호출합니다 GetMaxByteCount . 이 메서드는 GetByteCount 일반적으로 메모리를 더 적게 할당할 수 있지만 메서드는 GetMaxByteCount 일반적으로 더 빠르게 실행됩니다.
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.
추가 정보
적용 대상
GetBytes(String, Int32, Int32, Byte[], Int32)
파생 클래스에서 재정의되는 경우 지정된 문자열의 문자 집합을 지정된 바이트 배열로 인코딩합니다.
public:
virtual int GetBytes(System::String ^ s, int charIndex, int charCount, cli::array <System::Byte> ^ bytes, int byteIndex);
public virtual int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex);
abstract member GetBytes : string * int * int * byte[] * int -> int
override this.GetBytes : string * int * int * byte[] * int -> int
Public Overridable Function GetBytes (s As String, charIndex As Integer, charCount As Integer, bytes As Byte(), byteIndex As Integer) As Integer
매개 변수
- s
- String
인코딩할 문자 집합이 포함된 문자열입니다.
- charIndex
- Int32
인코딩할 첫 번째 문자의 인덱스입니다.
- charCount
- Int32
인코딩할 문자 수입니다.
- bytes
- Byte[]
결과 바이트 시퀀스를 포함할 바이트 배열입니다.
- byteIndex
- Int32
결과 바이트 시퀀스 작성을 시작할 인덱스입니다.
반품
에 기록된 실제 바이트 수입니다 bytes.
예외
charIndex 또는 charCountbyteIndex 0보다 작습니다.
-또는-
charIndex 에서 charCount 유효한 범위를 s나타내지 않습니다.
-또는-
byteIndex에서 유효한 인덱스가 아닌 경우 bytes
bytes 에는 결과 바이트를 수용할 수 있는 충분한 용량 byteIndex 이 없습니다.
예제
다음 예제에서는 문자열 또는 문자열의 범위를 인코딩하는 데 필요한 바이트 수를 결정하고 문자를 인코딩하고 결과 바이트를 표시합니다.
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
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub 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)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2) & ChrW(&HD8FF) & ChrW(&HDCFF)
' Get different encodings.
Dim u7 As Encoding = Encoding.UTF7
Dim u8 As Encoding = Encoding.UTF8
Dim u16LE As Encoding = Encoding.Unicode
Dim u16BE As Encoding = Encoding.BigEndianUnicode
Dim u32 As Encoding = 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)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(s.Length)
Console.Write(" {0,-3} :", iMBC)
' Encode the entire string.
Dim bytes As Byte() = enc.GetBytes(s)
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Overloads Public Shared Sub PrintCountsAndBytes(s As String, index As Integer, count As Integer, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-30} :", enc.ToString())
' Display the exact byte count.
Dim iBC As Integer = enc.GetByteCount(s.ToCharArray(), index, count)
Console.Write(" {0,-3}", iBC)
' Display the maximum byte count.
Dim iMBC As Integer = enc.GetMaxByteCount(count)
Console.Write(" {0,-3} :", iMBC)
' Encode a range of characters in the string.
' NOTE: In VB.NET, arrays contain one extra element by default.
' The following line creates the array with the exact number of elements required.
Dim bytes(iBC - 1) As Byte
enc.GetBytes(s, index, count, bytes, bytes.GetLowerBound(0))
' Display all the encoded bytes.
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
'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
설명
결과 바이트를 저장하는 데 필요한 GetBytes 정확한 배열 크기를 계산하려면 메서드를 GetByteCount 호출해야 합니다. 최대 배열 크기를 계산하려면 메서드를 호출합니다 GetMaxByteCount . 이 메서드는 GetByteCount 일반적으로 메모리를 더 적게 할당할 수 있지만 메서드는 GetMaxByteCount 일반적으로 더 빠르게 실행됩니다.
변환할 데이터를 순차 블록(예: 스트림에서 읽은 데이터)에서만 사용할 수 있거나, 데이터의 양이 너무 많아 더 작은 블록으로 나누어야 하는 경우에는 Decoder 메서드의 Encoder 또는 GetDecoder 메서드의 GetEncoder를 각각 제공하는 파생 클래스의 메서드를 사용해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 이 메서드는 Encoding.GetBytes 단일 입력 스트림에서 여러 변환을 Encoder.GetBytes 처리하는 메서드와 달리 불연속 변환을 예상합니다.
여러 버전의 GetByteCount 버전이 GetBytes 지원됩니다. 다음은 이러한 메서드를 사용하기 위한 몇 가지 프로그래밍 고려 사항입니다.
앱은 코드 페이지에 많은 입력 문자를 인코딩하고 여러 호출을 사용하여 문자를 처리해야 할 수 있습니다. 이 경우 사용 중인 개체에 의해 Encoder 유지되는 상태를 고려하여 호출 간에 상태를 유지해야 할 수 있습니다. 예를 들어 서로게이트 쌍을 포함하는 문자 시퀀스는 높은 서로게이트로 끝날 수 있습니다. 다음 Encoder 호출을 시작할 때 낮은 서로게이트와 결합될 수 있도록 높은 서로게이트를 기억합니다. 상태를 유지할 수 없으므로 문자가 .)로 EncoderFallback전송됩니다. Encoding
앱이 문자열 입력을 처리하는 경우 문자열 버전을 GetBytes사용해야 합니다.
유니코드 문자 버퍼 버전은 GetBytes(Char*, Int32, Byte*, Int32) 특히 개체를 사용 Encoder 하거나 기존 버퍼에 삽입하는 여러 호출을 통해 몇 가지 빠른 기술을 허용합니다. 그러나 포인터가 필요하기 때문에 이 메서드 버전은 안전하지 않을 수 있습니다.
앱이 대량의 데이터를 변환해야 하는 경우 출력 버퍼를 다시 사용해야 합니다. 이 경우 GetBytes 바이트 배열을 지원하는 버전이 가장 적합합니다.
대신 메서드를 Encoder.Convert 사용하는 것이 좋습니다 GetByteCount. 변환 메서드는 가능한 한 많은 데이터를 변환하고 출력 버퍼가 너무 작으면 예외를 throw합니다. 스트림의 연속 인코딩의 경우 이 메서드가 가장 적합한 경우가 많습니다.