Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

UTF8Encoding.GetByteCount Method

Calculates the number of bytes required to store the results of encoding a set of characters from a specified String or Unicode character array.

Overload List

Calculates the number of bytes required to store the results of encoding the characters from a specified String.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetByteCount(String) As Integer
[C#] public override int GetByteCount(string);
[C++] public: int GetByteCount(String*);
[JScript] public override function GetByteCount(String) : int;

Calculates the number of bytes required to store the results of encoding a set of characters from a specified Unicode character array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetByteCount(Char(), Integer, Integer) As Integer
[C#] public override int GetByteCount(char[], int, int);
[C++] public: int GetByteCount(__wchar_t __gc[], int, int);
[JScript] public override function GetByteCount(Char[], int, int) : int;

Inherited from Encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetByteCount(Char()) As Integer
[C#] public virtual int GetByteCount(char[]);
[C++] public: virtual int GetByteCount(__wchar_t __gc[]);
[JScript] public function GetByteCount(Char[]) : int;

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetByteCount method to return the number of bytes required to encode the array of Unicode characters chars using UTF8Encoding.

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

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf8 As New UTF8Encoding()
        Dim byteCount As Integer = utf8.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub 'Main
End Class 'UTF8EncodingExample

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

class UTF8EncodingExample {
    public static void Main() {
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        UTF8Encoding utf8 = new UTF8Encoding();
        int byteCount = utf8.GetByteCount(chars, 1, 2);
        Console.WriteLine(
            "{0} bytes needed to encode characters.", byteCount
        );
    }
}

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

int main()
{
   // Unicode characters.
   Char chars[] = 
   {
      L'\u03a0',  // Pi
      L'\u03a3',  // Sigma
      L'\u03a6',
      L'\u03a9'
   };

   UTF8Encoding* utf8 = new UTF8Encoding();
   int byteCount = utf8 -> GetByteCount(chars, 1, 2);
   Console::WriteLine(S"{0} bytes needed to encode characters.", __box(byteCount));
}

[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:
© 2015 Microsoft