Encoding: GetChars

GetChars

When implemented in a derived class, decodes all the bytes in the specified byte array into a set of characters.



 Public Function GetChars(
	  ByRef Bytes ( ) As Byte,
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As Integer ( ) :

Parameters

Bytes
[ByRef] Byte. The byte array containing the sequence of bytes to decode.
Index
[ByRef] Optional. Variant. The index of the first byte to decode.
Count
[ByRef] Optional. Variant. The number of bytes to decode.

Return Values

Integer() -  A character array containing the results of decoding the specified sequence of bytes.

Remarks

Encoding.GetChars gets characters from an input byte sequence. Encoding.GetChars 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 an implemented 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 GetChars method performs the actual decoding. The Encoding.GetChars 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 GetChars are supported. The following are some programming considerations for use of these methods:

Exceptions

ExceptionCondition
ArgumentNullException Bytes is null.
ArgumentOutOfRangeException Index is less than the lower-bound of Bytes.
-or-
Count is less than zero.
-or-
Index and Count do not denote a valid range in Bytes.
DecoderFallbackException A fallback occurred
-and-
DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example encodes a string into an array of bytes, and then decodes the bytes into an array of characters.

Public Sub Main()
    Dim U32LE As Encoding
    Dim U32BE As Encoding
    Dim MyStr As String
    Dim BytesLE() As Byte
    Dim BytesBE() As Byte
    
    Set Console.OutputEncoding = Encoding.UTF8
    Set U32LE = Encoding.GetEncoding("utf-32")
    Set U32BE = Encoding.GetEncoding("utf-32BE")
    
    ' Use a string containing the following characters:
    '    Latin Small Letter Z (U+007A)
    '    Latin Small Letter A (U+0061)
    '    Combining Breve (U+0306)
    '    Latin Small Letter AE With Acute (U+01FD)
    '    Greek Small Letter Beta (U+03B2)
    MyStr = "za" & ChrW$(&H306) & ChrW$(&H1FD) & ChrW$(&H3B2)
    
    ' Encode the string using the big-endian byte order.
    BytesBE = U32BE.GetBytes(MyStr)
    
    ' Encode the string using the little-endian byte order.
    BytesLE = U32LE.GetBytes(MyStr)
    
    ' Get the char counts, and decode the byte arrays.
    Console.WriteValue "BE array with BE encoding : "
    PrintCountsAndChars BytesBE, U32BE
    Console.WriteValue "LE array with LE encoding : "
    PrintCountsAndChars BytesLE, U32LE
    
    Console.ReadKey
End Sub

Private Sub PrintCountsAndChars(ByRef Bytes() As Byte, ByVal Enc As Encoding)
    Dim CharCount As Long
    Dim MaxCount As Long
    Dim Chars() As Integer
    
    ' Display the name of the encoding used.
    Console.WriteValue "{0,-25} :", Enc.ToString
    
    ' Display the exact character count.
    CharCount = Enc.GetCharCount(Bytes)
    Console.WriteValue " {0,-3}", CharCount
    
    ' Display the maximum character count.
    MaxCount = Enc.GetMaxCharCount(CorArray.Length(Bytes))
    Console.WriteValue " {0,-3} :", MaxCount
    
    ' Decode the bytes and display the characters.
    Chars = Enc.GetChars(Bytes)
    Console.WriteLine NewString(Chars)
End Sub

' This example code produces the following output.
'
'BE array with BE encoding : CorLib.UTF32Encoding      : 5   12  :zăǽβ
'LE array with LE encoding : CorLib.UTF32Encoding      : 5   12  :zăǽβ

See Also

Project CorLib Overview

Class Encoding Overview

GetCharsEx

GetCharCount

GetMaxCharCount

GetDecoder

GetString