This documentation is archived and is not being maintained.

UnicodeEncoding.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 UnicodeEncoding.

[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 UnicodeEncodingExample
    
    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 uni As New UnicodeEncoding()
        Dim byteCount As Integer = uni.GetByteCount(chars, 1, 2)
        Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
    End Sub 'Main
End Class 'UnicodeEncodingExample

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

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

        UnicodeEncoding Unicode = new UnicodeEncoding();
        int byteCount = Unicode.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'
   };

   UnicodeEncoding* Unicode = new UnicodeEncoding();
   int byteCount = Unicode -> 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

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace

Show: