UnicodeEncoding.GetByteCount Method (Char[], Int32, Int32)

Calculates the number of bytes produced by encoding a set of characters from the specified character array.

Namespace:  System.Text
Assemblies:   System.Text.Encoding.Extensions (in System.Text.Encoding.Extensions.dll)
  mscorlib (in mscorlib.dll)

public override int GetByteCount(
	char[] chars,
	int index,
	int count
)

Parameters

chars
Type: System.Char[]

The character array containing the set of characters to encode.

index
Type: System.Int32

The index of the first character to encode.

count
Type: System.Int32

The number of characters to encode.

Return Value

Type: System.Int32
The number of bytes produced by encoding the specified characters.

ExceptionCondition
ArgumentNullException

chars is null (Nothing).

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in chars.

-or-

The resulting number of bytes is greater than the maximum number that can be returned as an integer.

ArgumentException

Error detection is enabled, and chars contains an invalid sequence of characters.

EncoderFallbackException

A fallback occurred (see Character Encoding in the .NET Framework for fuller explanation)

-and-

EncoderFallback is set to EncoderExceptionFallback.

To calculate the exact array size required by GetBytes to store the resulting bytes, the application uses GetByteCount. To calculate the maximum array size, you call the GetMaxByteCount method. The GetByteCount method generally allocates less memory, while the GetMaxByteCount method generally executes faster.

With error detection enabled, an invalid sequence causes this method to throw an ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix a stream of encoded bytes with a preamble. Inserting the preamble at the beginning of a byte stream (such as at the beginning of a series of bytes to be written to a file) is the developer's responsibility, and the number of bytes in the preamble Is not reflected in the value returned by the GetByteCount(Char[], Int32, Int32) method.

The following example populates an array with a Latin uppercase and lowercase characters and calls the GetByteCount(Char[], Int32, Int32) method to determine the number of bytes needed to encode the Latin lowercase characters. It then displays this information along with the total number of bytes needed if a byte order mark is added. It compares this number with the value returned by the GetMaxByteCount method, which indicates maximum number of bytes needed to encode the Latin lowercase characters. The following example populates an array with a combination of Greek and Cyrillic characters and calls the GetByteCount(Char[], Int32, Int32) method to determine the number of bytes needed to encode the Cyrillic characters. It then displays this information along with the total number of bytes needed if a byte order mark is added. It compares this number with the value returned by the GetMaxByteCount method, which indicates maximum number of bytes needed to encode the Cyrillic characters.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft