UnicodeEncoding.GetByteCount 메서드

정의

문자 집합을 인코딩할 경우 생성되는 바이트 수를 계산합니다.

오버로드

GetByteCount(String)

지정한 문자열의 문자를 인코딩하여 생성되는 바이트 수를 계산합니다.

GetByteCount(Char*, Int32)

지정한 문자 포인터에서 시작되는 문자 집합을 인코딩할 경우 생성되는 바이트 수를 계산합니다.

GetByteCount(Char[], Int32, Int32)

지정한 문자 배열의 문자 집합을 인코딩할 경우 생성되는 바이트 수를 계산합니다.

GetByteCount(String)

지정한 문자열의 문자를 인코딩하여 생성되는 바이트 수를 계산합니다.

public:
 override int GetByteCount(System::String ^ s);
public override int GetByteCount (string s);
override this.GetByteCount : string -> int
Public Overrides Function GetByteCount (s As String) As Integer

매개 변수

s
String

인코딩할 문자 집합이 포함된 문자열입니다.

반환

Int32

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

예외

s이(가) null인 경우

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

오류 검색이 사용되고 s에 잘못된 문자 시퀀스를 포함합니다.

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

예제

다음 예제에서는 문자열을 GetMaxByteCount 인코딩하는 데 필요한 최대 및 실제 바이트 수를 계산하는 메서드 및 GetByteCount(String) 메서드를 호출합니다. 또한 바이트 순서 표시를 사용하여 바이트 스트림을 저장하는 데 필요한 실제 바이트 수를 표시합니다.

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF-16 Encoding Example";
        Encoding unicode = Encoding.Unicode;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF-16 Encoding Example':
//          Maximum:         48
//          Actual:          46
//          Actual with BOM: 48
Imports System.Text

Module Example
    Public Sub Main()
        Dim chars As String = "UTF-16 Encoding Example"
        Dim unicode As Encoding = Encoding.Unicode

        Console.WriteLine("Bytes needed to encode '{0}':", chars)
        Console.WriteLine("   Maximum:         {0}",
                          unicode.GetMaxByteCount(chars.Length))
        Console.WriteLine("   Actual:          {0}",
                          unicode.GetByteCount(chars))
        Console.WriteLine("   Actual with BOM: {0}",
                          unicode.GetByteCount(chars) + unicode.GetPreamble().Length)
    End Sub
End Module
' The example displays the following output:
'       Bytes needed to encode 'UTF-16 Encoding Example':
'          Maximum:         48
'          Actual:          46
'          Actual with BOM: 48

설명

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

오류 검색 시퀀스가 잘못되면 이 메서드가 .를 throw합니다 ArgumentException. 오류 검색이 없으면 잘못된 시퀀스가 무시되고 예외가 throw되지 않습니다.

중요

인코딩된 바이트가 파일 또는 스트림으로 저장될 때 제대로 디코딩되도록 하려면 인코딩된 바이트 스트림의 접두사를 프리앰블으로 접두사로 지정할 수 있습니다. 바이트 스트림의 시작 부분에 프리앰블을 삽입하는 것은 개발자의 책임이며, 프리앰블의 바이트 수는 메서드에서 반환 GetByteCount(String) 된 값에 반영되지 않습니다.

추가 정보

적용 대상

GetByteCount(Char*, Int32)

중요

이 API는 CLS 규격이 아닙니다.

지정한 문자 포인터에서 시작되는 문자 집합을 인코딩할 경우 생성되는 바이트 수를 계산합니다.

public:
 override int GetByteCount(char* chars, int count);
[System.CLSCompliant(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public override int GetByteCount (char* chars, int count);
[<System.CLSCompliant(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetByteCount : nativeptr<char> * int -> int

매개 변수

chars
Char*

인코딩할 첫 번째 문자를 가리키는 포인터입니다.

count
Int32

인코딩할 문자 수입니다.

반환

Int32

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

특성

예외

chars이(가) null인 경우

count가 0보다 작은 경우

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

오류 검색 기능을 사용하고 있으며 chars에 잘못된 문자 시퀀스가 포함되어 있는 경우

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

설명

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

오류 검색 시퀀스가 잘못되면 이 메서드가 .를 throw합니다 ArgumentException. 오류 검색이 없으면 잘못된 시퀀스가 무시되고 예외가 throw되지 않습니다.

중요

인코딩된 바이트가 파일 또는 스트림으로 저장될 때 제대로 디코딩되도록 하려면 인코딩된 바이트 스트림의 접두사를 프리앰블으로 접두사로 지정할 수 있습니다. 바이트 스트림의 시작 부분에 프리앰블을 삽입하는 것은 개발자의 책임이며, 프리앰블의 바이트 수는 메서드에서 반환 GetByteCount 된 값에 반영되지 않습니다.

추가 정보

적용 대상

GetByteCount(Char[], Int32, Int32)

지정한 문자 배열의 문자 집합을 인코딩할 경우 생성되는 바이트 수를 계산합니다.

public:
 override int GetByteCount(cli::array <char> ^ chars, int index, int count);
public override int GetByteCount (char[] chars, int index, int count);
override this.GetByteCount : char[] * int * int -> int
Public Overrides Function GetByteCount (chars As Char(), index As Integer, count As Integer) As Integer

매개 변수

chars
Char[]

인코딩할 문자 집합이 포함된 문자 배열입니다.

index
Int32

인코딩할 첫 번째 문자의 인덱스입니다.

count
Int32

인코딩할 문자 수입니다.

반환

Int32

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

예외

charsnull(Nothing)입니다.

index 또는 count가 0보다 작습니다.

또는 indexcountchars에서 올바른 범위를 나타내지 않습니다.

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

오류 검색이 사용되고 chars에 잘못된 문자 시퀀스를 포함합니다.

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

예제

다음 예제에서는 라틴어 대문자와 소문자로 배열을 채우고 메서드를 호출 GetByteCount(Char[], Int32, Int32) 하여 라틴어 소문자를 인코딩하는 데 필요한 바이트 수를 결정합니다. 그런 다음 바이트 순서 표시가 추가된 경우 필요한 총 바이트 수와 함께 이 정보를 표시합니다. 이 숫자를 메서드에서 반환된 GetMaxByteCount 값과 비교합니다. 이 값은 라틴어 소문자를 인코딩하는 데 필요한 최대 바이트 수를 나타냅니다. 다음 예제에서는 그리스어와 키릴 자모 문자의 조합으로 배열을 채우고 키릴 자모 문자를 인코딩하는 데 필요한 바이트 수를 결정하는 메서드를 호출 GetByteCount(Char[], Int32, Int32) 합니다. 그런 다음 바이트 순서 표시가 추가된 경우 필요한 총 바이트 수와 함께 이 정보를 표시합니다. 키릴 자모 문자를 인코딩하는 데 필요한 최대 바이트 수를 나타내는 메서드에서 반환 GetMaxByteCount 된 값과 이 숫자를 비교합니다.

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      int uppercaseStart = 0x0041;
      int uppercaseEnd = 0x005a;
      int lowercaseStart = 0x0061;
      int lowercaseEnd = 0x007a;
      // Instantiate a UTF8 encoding object with BOM support.
      Encoding unicode = Encoding.Unicode;

      // Populate array with characters.
      char[] chars = new char[lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 2];
      int index = 0;
      for (int ctr = uppercaseStart; ctr <= uppercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }
      for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
         chars[index] = (char)ctr;
         index++;
      }

      // Display the bytes needed for the lowercase characters.
      Console.WriteLine("Bytes needed for lowercase Latin characters:");
      Console.WriteLine("   Maximum:         {0,5:N0}",
                        unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
                                          unicode.GetPreamble().Length);
   }
}
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:            54
//          Actual:             52
//          Actual with BOM:    54
Imports System.Text

Module Example
   Public Sub Main()
      Dim uppercaseStart As Integer = &h0041
      Dim uppercaseEnd As Integer = &h005a
      Dim lowercaseStart As Integer = &h0061
      Dim lowercaseEnd As Integer = &h007a
      ' Instantiate a UTF8 encoding object with BOM support.
      Dim unicode As Encoding = Encoding.Unicode
      
      ' Populate array with characters.
      Dim chars(lowercaseEnd - lowercaseStart + uppercaseEnd - uppercaseStart + 1) As Char
      Dim index As Integer = 0
      For ctr As Integer = uppercaseStart To uppercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next
      For ctr As Integer = lowercaseStart To lowercaseEnd
         chars(index) = ChrW(ctr)
         index += 1
      Next

      ' Display the bytes needed for the lowercase characters.
        Console.WriteLine("Bytes needed for lowercase Latin characters:")
        Console.WriteLine("   Maximum:         {0,5:N0}",
                          unicode.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual:          {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1))
        Console.WriteLine("   Actual with BOM: {0,5:N0}",
                          unicode.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                            lowercaseEnd - lowercaseStart + 1) +
                                            unicode.GetPreamble().Length)
   End Sub
End Module
' The example displays the following output:
'       Bytes needed for lowercase Latin characters:
'          Maximum:            54
'          Actual:             52
'          Actual with BOM:    54

설명

에 필요한 정확한 배열 크기를 계산 하려면 GetBytes 애플리케이션에서는 결과 바이트를 저장 하려면 GetByteCount합니다. 최대 배열 크기를 계산하려면 메서드를 호출합니다 GetMaxByteCount . 메서드는 GetByteCount 일반적으로 더 적은 메모리를 할당하지만 메서드는 GetMaxByteCount 일반적으로 더 빠르게 실행됩니다.

오류 검색을 사용하도록 설정하면 잘못된 시퀀스로 인해 이 메서드가 을 throw합니다 ArgumentException. 오류 검색이 없으면 잘못된 시퀀스가 무시되고 예외가 throw되지 않습니다.

인코딩된 바이트가 파일 또는 스트림으로 저장될 때 제대로 디코딩되도록 하려면 인코딩된 바이트 스트림의 접두사를 프리앰블으로 접두사로 지정할 수 있습니다. 바이트 스트림의 시작 부분에 프리앰블을 삽입하는 것은 개발자의 책임이며, 프리앰블의 바이트 수는 메서드에서 반환 GetByteCount(Char[], Int32, Int32) 된 값에 반영되지 않습니다.

추가 정보

적용 대상