UTF8Encoding.GetMaxCharCount(Int32) 메서드

정의

지정한 수의 바이트를 디코딩할 경우 생성되는 최대 문자 수를 계산합니다.

public:
 override int GetMaxCharCount(int byteCount);
public override int GetMaxCharCount (int byteCount);
override this.GetMaxCharCount : int -> int
Public Overrides Function GetMaxCharCount (byteCount As Integer) As Integer

매개 변수

byteCount
Int32

디코딩할 바이트 수입니다.

반환

Int32

지정한 수의 바이트를 디코딩할 경우 생성되는 최대 문자 수입니다.

예외

byteCount가 0보다 작은 경우

또는 결과 바이트 수가 정수로 반환될 수 있는 최대 수보다 큽니다.

대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조). 및 DecoderFallbackDecoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 메서드를 GetMaxCharCount 사용하여 지정된 바이트 수를 디코딩하여 생성된 최대 문자 수를 반환합니다.

using namespace System;
using namespace System::Text;
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   int byteCount = 8;
   int maxCharCount = utf8->GetMaxCharCount( byteCount );
   Console::WriteLine( "Maximum of {0} characters needed to decode {1} bytes.", maxCharCount, byteCount );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        int byteCount = 8;
        int maxCharCount = utf8.GetMaxCharCount(byteCount);
        Console.WriteLine(
            "Maximum of {0} characters needed to decode {1} bytes.",
            maxCharCount,
            byteCount
        );
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim byteCount As Integer = 8
        Dim maxCharCount As Integer = utf8.GetMaxCharCount(byteCount)
        Console.WriteLine( _
            "Maximum of {0} characters needed to decode {1} bytes.", _
            maxCharCount, _
            byteCount _
        )
    End Sub
End Class

설명

결과 문자를 저장하는 데 필요한 GetChars 정확한 배열 크기를 계산하려면 메서드를 호출합니다 GetCharCount . 최대 배열 크기를 계산하려면 메서드를 호출합니다 GetMaxCharCount . 메서드는 GetCharCount 일반적으로 더 적은 메모리를 할당하지만 메서드는 GetMaxCharCount 일반적으로 더 빠르게 실행됩니다.

GetMaxCharCount 는 현재 선택된 DecoderFallback최악의 경우를 포함하여 최악의 경우 숫자입니다. 잠재적으로 큰 문자열 GetMaxCharCount 을 사용하여 대체를 선택한 경우 큰 값을 반환할 수 있습니다.

대부분의 경우 이 메서드는 작은 문자열에 적합한 숫자를 반환합니다. 큰 문자열의 경우 매우 큰 버퍼를 사용하고 좀 더 적절한 버퍼를 초과하는 드문 경우에서 오류를 catch하는 중에서 선택해야 할 수 있습니다. 또는을 사용 하 여 다른 방법을 고려해 볼 수도 GetCharCount 있습니다 Encoder.Convert .

GetMaxCharCount에는 관계가 없습니다 GetBytes . 애플리케이션에 사용 하는 유사한 함수가 필요 하는 경우 GetBytes를 사용 해야 GetMaxByteCount합니다.

참고

GetMaxCharCount(N)이 반드시와 동일한 값은 아닙니다 N* GetMaxCharCount(1) .

적용 대상

추가 정보