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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

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


Type: System.Char[]

The character array containing the set of characters to encode.

Type: System.Int32

The index of the first character to encode.

Type: System.Int32

The number of characters to encode.

Return Value

Type: System.Int32

The number of bytes produced by encoding the specified characters.

Exception Condition

chars is null.


index or count is less than zero.


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


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


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


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


The EncoderFallback property is set to EncoderExceptionFallback.

To calculate the exact array size required by GetBytes to store the resulting bytes, you call the uses GetByteCount method. 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, an invalid sequence causes this method to throw an ArgumentException exception. 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 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.

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 utf8 = new UTF8Encoding(true);

      // 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;
      for (int ctr = lowercaseStart; ctr <= lowercaseEnd; ctr++) {
         chars[index] = (char)ctr;

      // Display the bytes needed for the lowercase characters.
      Console.WriteLine("Bytes needed for lowercase Latin characters:");
      Console.WriteLine("   Maximum:         {0,5:N0}",
                        utf8.GetMaxByteCount(lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual:          {0,5:N0}",
                        utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1));
      Console.WriteLine("   Actual with BOM: {0,5:N0}",
                        utf8.GetByteCount(chars, uppercaseEnd - uppercaseStart + 1,
                                          lowercaseEnd - lowercaseStart + 1) +
// The example displays the following output:
//       Bytes needed for lowercase Latin characters:
//          Maximum:            81
//          Actual:             26
//          Actual with BOM:    29

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top