Encoding.GetMaxCharCount(Int32) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
파생 클래스에서 재정의되는 경우 지정된 바이트 수를 디코딩하여 생성된 최대 문자 수를 계산합니다.
public:
abstract int GetMaxCharCount(int byteCount);
public abstract int GetMaxCharCount(int byteCount);
abstract member GetMaxCharCount : int -> int
Public MustOverride Function GetMaxCharCount (byteCount As Integer) As Integer
매개 변수
- byteCount
- Int32
디코딩할 바이트 수입니다.
반품
지정된 바이트 수를 디코딩하여 생성되는 최대 문자 수입니다.
예외
byteCount가 0보다 작습니다.
예제
다음 예제에서는 문자열을 바이트 배열로 인코딩한 다음 바이트를 문자 배열로 디코딩합니다.
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Encoding u32LE = Encoding.GetEncoding( "utf-32" );
Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );
// Use a string containing the following characters:
// 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)
String myStr = "za\u0306\u01FD\u03B2";
// Encode the string using the big-endian byte order.
byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );
// Encode the string using the little-endian byte order.
byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );
// Get the char counts, and decode the byte arrays.
Console.Write( "BE array with BE encoding : " );
PrintCountsAndChars( barrBE, u32BE );
Console.Write( "LE array with LE encoding : " );
PrintCountsAndChars( barrLE, u32LE );
}
public static void PrintCountsAndChars( byte[] bytes, Encoding enc ) {
// Display the name of the encoding used.
Console.Write( "{0,-25} :", enc.ToString() );
// Display the exact character count.
int iCC = enc.GetCharCount( bytes );
Console.Write( " {0,-3}", iCC );
// Display the maximum character count.
int iMCC = enc.GetMaxCharCount( bytes.Length );
Console.Write( " {0,-3} :", iMCC );
// Decode the bytes and display the characters.
char[] chars = enc.GetChars( bytes );
Console.WriteLine( chars );
}
}
/*
This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
BE array with BE encoding : System.Text.UTF32Encoding : 5 12 :zăǽβ
LE array with LE encoding : System.Text.UTF32Encoding : 5 12 :zăǽβ
*/
Imports System.Text
Public Class SamplesEncoding
Public Shared Sub Main()
' Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
Dim u32LE As Encoding = Encoding.GetEncoding("utf-32")
Dim u32BE As Encoding = Encoding.GetEncoding("utf-32BE")
' Use a string containing the following characters:
' 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)
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)
' Encode the string using the big-endian byte order.
' 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 barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)
' Encode the string using the little-endian byte order.
' 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 barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)
' Get the char counts, and decode the byte arrays.
Console.Write("BE array with BE encoding : ")
PrintCountsAndChars(barrBE, u32BE)
Console.Write("LE array with LE encoding : ")
PrintCountsAndChars(barrLE, u32LE)
End Sub
Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)
' Display the name of the encoding used.
Console.Write("{0,-25} :", enc.ToString())
' Display the exact character count.
Dim iCC As Integer = enc.GetCharCount(bytes)
Console.Write(" {0,-3}", iCC)
' Display the maximum character count.
Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
Console.Write(" {0,-3} :", iMCC)
' Decode the bytes and display the characters.
Dim chars As Char() = enc.GetChars(bytes)
Console.WriteLine(chars)
End Sub
End Class
'This code produces the following output. The question marks take the place of characters that cannot be displayed at the console.
'
'BE array with BE encoding : System.Text.UTF32Encoding : 5 12 :zăǽβ
'LE array with LE encoding : System.Text.UTF32Encoding : 5 12 :zăǽβ
설명
결과 문자를 저장하는 데 필요한 GetChars 정확한 배열 크기를 계산하려면 이 메서드를 GetCharCount 사용해야 합니다. 최대 배열 크기를 계산하려면 메서드를 GetMaxCharCount 사용합니다. 이 메서드는 GetCharCount 일반적으로 메모리를 더 적게 할당할 수 있지만 메서드는 GetMaxCharCount 일반적으로 더 빠르게 실행됩니다.
GetMaxCharCount 는 현재 선택된 최악의 경우를 포함하여 최악의 경우를 검색합니다 DecoderFallback. 잠재적으로 큰 문자열 GetMaxCharCount 을 사용하여 대체를 선택하면 큰 값을 검색합니다.
대부분의 경우 이 메서드는 작은 문자열에 대해 적절한 숫자를 검색합니다. 큰 문자열의 경우 매우 큰 버퍼를 사용하고 좀 더 합리적인 버퍼가 너무 작은 경우 오류를 catch하는 중에서 선택해야 할 수 있습니다. 사용 또는 GetCharCount.를 사용하는 Decoder.Convert 다른 방법을 고려할 수도 있습니다.
GetMaxCharCount 에 대한 관계가 GetBytes없습니다. 사용할 GetBytesGetMaxByteCount유사한 함수가 필요한 경우 .
사용하는 GetMaxCharCount경우 입력 버퍼의 최대 크기에 따라 출력 버퍼를 할당해야 합니다. 출력 버퍼의 크기가 제한되는 경우 이 메서드를 Decoder.Convert 사용할 수 있습니다.
GetMaxCharCount 이전 인코더 작업에서 남은 바이트에 대한 최악의 경우를 고려합니다. 대부분의 코드 페이지에서 이 메서드에 0 값을 전달하면 1보다 크거나 같은 값이 검색됩니다.
메모
GetMaxCharCount(N) 가 반드시 .와 N* GetMaxCharCount(1)같은 값은 아닙니다.
구현자 참고
모든 Encoding 구현은 버퍼가 이 메서드의 계산 결과에 따라 크기가 조정되는 경우 버퍼 오버플로 예외가 발생하지 않도록 보장해야 합니다.