Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

UTF8Encoding::GetByteCount Method (array<Char>^, Int32, Int32)

 

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:
virtual int GetByteCount(
	array<wchar_t>^ chars,
	int index,
	int count
) override

Parameters

chars
Type: array<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.

Exception Condition
ArgumentNullException

chars is null.

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 complete explanation)

-and-

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(array<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.

No code example is currently available or this language may not be supported.

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