Encoding: GetCharsEx

GetCharsEx

When implemented in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.



 Public Function GetCharsEx(
	  ByRef Bytes ( ) As Byte,
	  ByVal ByteIndex As Long,
	  ByVal ByteCount As Long,
	  ByRef Chars ( ) As Integer,
	  ByVal CharIndex As Long ) As Long

Parameters

Bytes
[ByRef] Byte. The byte array containing the sequence of bytes to decode.
ByteIndex
[ByVal] Long. The index of the first byte to decode.
ByteCount
[ByVal] Long. The number of bytes to decode.
Chars
[ByRef] Integer. The character array to contain the resulting set of characters.
CharIndex
[ByVal] Long. The index at which to start writing the resulting set of characters.

Return Values

Long -  The actual number of characters written into Chars.

Remarks

To calculate the exact array size required by GetCharsEx to store the resulting characters, the application should use GetCharCount. To calculate the maximum array size, the application should use GetMaxCharCount. The GetCharCount method generally allows allocation of less memory, while the GetMaxCharCount method generally executes faster.

Encoding.GetCharsEx gets characters from an input byte sequence. Encoding.GetCharsEx is different than Decoder.GetChars because Encoding expects discrete conversions, while Decoder is designed for multiple passes on a single input stream.

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.

Note This method is intended to operate on Unicode characters, not on arbitrary binary data, such as byte arrays. If your application needs to encode arbitrary binary data into text, it should use a protocol such as uuencode, which is implemented by methods such as Convert.ToBase64CharArray.

The GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetCharsEx method performs the actual decoding. The Encoding.GetCharsEx method expects discrete conversions, in contrast to the Decoder.GetChars method, which handles multiple passes on a single input stream.

Several versions of GetCharCount and GetCharsEx are supported. The following are some programming considerations for use of these methods:

Exceptions

ExceptionCondition
ArgumentNullException Bytes is uninitialized.
-or-
Chars is uninitialized.
ArgumentOutOfRangeException ByteIndex is less than the lower-bound of Bytes.
-or-
CharIndex is less than the lower-bound of Chars.
-or-
ByteCount is less than zero.
-or-
ByteIndex and ByteCount do not denote a valie range in Bytes.
-or-
CharIndex is not a valid index in Chars
ArgumentException Chars does not have enough capacity from CharIndex to the end of the array to accommodate the resulting characters.
DecoderFallbackException A fallback occurred
-and-
DecoderFallback is set to DecoderExceptionFallback.

Examples

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

Public Sub Main()
    Dim UnicodeString   As String
    Dim ASCII           As Encoding
    Dim Unicode         As Encoding
    Dim UnicodeBytes()  As Byte
    Dim ASCIIBytes()    As Byte
    Dim ASCIIChars()    As Integer
    Dim ASCIIString     As String
    
    Set Console.OutputEncoding = Encoding.UTF8
    UnicodeString = "This string contains the unicode character Pi (" & ChrW$(&H3A0) & ")"

    ' Create two different encodings.
    Set ASCII = Encoding.ASCII
    Set Unicode = Encoding.Unicode

    ' Convert the string into a byte array.
    UnicodeBytes = Unicode.GetBytes(UnicodeString)
    
    ' Perform the conversion from one encoding to the other.
    ASCIIBytes = Encoding.Convert(Unicode, ASCII, UnicodeBytes)
    
    ' Convert the new byte array into a char array and then into a string.
    ReDim ASCIIChars(0 To ASCII.GetCharCount(ASCIIBytes) - 1)
    
    ASCII.GetCharsEx ASCIIBytes, 0, CorArray.Length(ASCIIBytes), ASCIIChars, 0
    ASCIIString = NewString(ASCIIChars)
    
    ' Display the strings created before and after the conversion.
    Console.WriteLine "Original string: {0}", UnicodeString
    Console.WriteLine "Ascii converted string: {0}", ASCIIString
    
    Console.ReadKey
End Sub

' This example code produces the following output.
'
'    Original string: This string contains the unicode character Pi (Π)
'    Ascii converted string: This string contains the unicode character Pi (?)

See Also

Project CorLib Overview

Class Encoding Overview

GetChars

GetCharCount

GetMaxCharCount

GetDecoder

GetString