C# Encoding GetCharCount(Byte[], Int32, Int32)

Description

Encoding GetCharCount(Byte[], Int32, Int32) When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

Syntax

Encoding.GetCharCount(Byte[], Int32, Int32) has the following syntax.


public abstract int GetCharCount(
  byte[] bytes,//  w ww  .  jav a2  s . c  om
  int index,
  int count
)

Parameters

Encoding.GetCharCount(Byte[], Int32, Int32) has the following parameters.

  • bytes - The byte array containing the sequence of bytes to decode.
  • index - The index of the first byte to decode.
  • count - The number of bytes to decode.

Returns

Encoding.GetCharCount(Byte[], Int32, Int32) method returns The number of characters produced by decoding the specified sequence of bytes.

Example

The following example converts a string from one encoding to another.


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

class Example
{
    static void Main()
    {
        string unicodeString = "This string contains the unicode character Pi (\u03a0)";
        Encoding ascii = Encoding.ASCII;
        Encoding unicode = Encoding.Unicode;

        byte[] unicodeBytes = unicode.GetBytes(unicodeString);

        byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);


        int iCC = ascii.GetCharCount(asciiBytes, 0, 3);
        Console.Write(" {0,-3}", iCC);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding