C# ASCIIEncoding GetByteCount(Char[])

Description

ASCIIEncoding GetByteCount(Char[]) When overridden in a derived class, calculates the number of bytes produced by encoding all the characters in the specified character array.

Syntax

ASCIIEncoding.GetByteCount(Char[]) has the following syntax.


public virtual int GetByteCount(
  char[] chars
)

Parameters

ASCIIEncoding.GetByteCount(Char[]) has the following parameters.

  • chars - The character array containing the characters to encode.

Returns

ASCIIEncoding.GetByteCount(Char[]) method returns The number of bytes produced by encoding all the characters in the specified character array.

Example


using System;// w  w w . j a  v  a2  s  . c o  m
using System.Text;

public class SamplesEncoding
{

    public static void Main()
    {
        char[] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };

        ASCIIEncoding enc = new ASCIIEncoding();

        Console.Write("{0,-30} :", enc.ToString());
        int iBC = enc.GetByteCount(chars);
        Console.Write(" {0,-3}", iBC);
        int iMBC = enc.GetMaxByteCount(chars.Length);
        Console.Write(" {0,-3} :", iMBC);
        byte[] bytes = enc.GetBytes(chars);
        for (int i = 0; i < bytes.Length; i++)
            Console.Write("{0:X2} ", bytes[i]);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding