GetByteCount Method (String)
Collapse the table of content
Expand the table of content

UTF32Encoding.GetByteCount Method (String)

 

Calculates the number of bytes produced by encoding the characters in the specified String.

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

public override int GetByteCount(
	string s
)

Parameters

s
Type: System.String

The String containing the set of characters to encode.

Return Value

Type: System.Int32

The number of bytes produced by encoding the specified characters.

Exception Condition
ArgumentNullException

s is null.

ArgumentOutOfRangeException

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

ArgumentException

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

EncoderFallbackException

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

-and-

EncoderFallback is set to EncoderExceptionFallback.

To calculate the exact array size required by GetBytes to store the resulting bytes, you call the 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 a 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 method.

The following example calls the GetMaxByteCount and GetByteCount(String) methods to calculate the maximum and actual number of bytes required to encode a string. It also displays the actual number of bytes required to store a byte stream with a byte order mark.

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        String chars = "UTF-32 Encoding Example";
        Encoding enc = Encoding.UTF32;

        Console.WriteLine("Bytes needed to encode '{0}':", chars);
        Console.WriteLine("   Maximum:         {0}",
                          enc.GetMaxByteCount(chars.Length));
        Console.WriteLine("   Actual:          {0}",
                          enc.GetByteCount(chars));
        Console.WriteLine("   Actual with BOM: {0}",
                          enc.GetByteCount(chars) + enc.GetPreamble().Length);
    }
}
// The example displays the following output:
//       Bytes needed to encode 'UTF-32 Encoding Example':
//          Maximum:         96
//          Actual:          92
//          Actual with BOM: 96

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft