This documentation is archived and is not being maintained.

UTF8Encoding.GetCharCount Method

Calculates the number of characters that would result from decoding a set of elements in a byte array.

Overload List

Calculates the number of characters that would result from decoding a specified range of elements in a byte array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetCharCount(Byte(), Integer, Integer) As Integer
[C#] public override int GetCharCount(byte[], int, int);
[C++] public: int GetCharCount(unsigned char __gc[], int, int);
[JScript] public override function GetCharCount(Byte[], int, int) : int;

Inherited from Encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetCharCount(Byte()) As Integer
[C#] public virtual int GetCharCount(byte[]);
[C++] public: virtual int GetCharCount(unsigned char __gc[]);
[JScript] public function GetCharCount(Byte[]) : int;

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetCharCount method to return the number of characters that would result from decoding a range of elements in the byte array bytes using a UTF8Encoding.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetCharCount. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 8)
        Console.WriteLine("{0} characters needed to decode bytes.", charCount)
    End Sub 'Main
End Class 'UTF8EncodingExample

[C#] 
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();
        int charCount = utf8.GetCharCount(bytes, 2, 8);
        Console.WriteLine(
            "{0} characters needed to decode bytes.", charCount
        );
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main()
{
   Byte bytes[] = 
   {
      85,  84,  70,  56,  32,  69, 110,
      99, 111, 100, 105, 110, 103,  32,
      69, 120,  97, 109, 112, 108, 101
   };

   UTF8Encoding* utf8 = new UTF8Encoding();
   int charCount = utf8 -> GetCharCount(bytes, 2, 8);
   Console::WriteLine(S"{0} characters needed to decode bytes.", __box(charCount));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

UTF8Encoding Class | UTF8Encoding Members | System.Text Namespace

Show: