This documentation is archived and is not being maintained.

UTF8Encoding.GetChars Method

Decodes a set of elements from a specified byte array and stores the result into a specified set of elements in a Unicode character array.

Overload List

Decodes a range of elements from a specified byte array and stores the result into a specified range of elements in a Unicode character array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetChars(Byte(), Integer, Integer, Char(), Integer) As Integer
[C#] public override int GetChars(byte[], int, int, char[], int);
[C++] public: int GetChars(unsigned char __gc[], int, int, __wchar_t __gc[], int);
[JScript] public override function GetChars(Byte[], int, int, Char[], int) : int;

Inherited from Encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetChars(Byte()) As Char()
[C#] public virtual char[] GetChars(byte[]);
[C++] public: virtual __wchar_t GetChars(unsigned char __gc[]) __gc[];
[JScript] public function GetChars(Byte[]) : Char[];

Inherited from Encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetChars(Byte(), Integer, Integer) As Char()
[C#] public virtual char[] GetChars(byte[], int, int);
[C++] public: virtual __wchar_t GetChars(unsigned char __gc[], int, int) __gc[];
[JScript] public function GetChars(Byte[], int, int) : Char[];

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetChars method to decode a range of elements in the byte array bytes and store the result in the character array chars. The GetCharCount method is used to calculate the number of characters needed to store the decoded elements.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetChars. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = { _
            85,  84,  70,  56,  32,  69, 110, _
            99, 111, 100, 105, 110, 103,  32, _
            69, 120,  97, 109, 112, 108, 101 _
        }
        
        Dim utf8 As New UTF8Encoding()
        
        Dim charCount As Integer = utf8.GetCharCount(bytes, 2, 13)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = utf8.GetChars(bytes, 2, 13, chars, 0)
        
        Console.WriteLine("{0} characters used to decode bytes.", charsDecodedCount)
        
        Console.Write("Decoded chars: ")
        Dim c As Char
        For Each c In  chars
            Console.Write("[{0}]", c)
        Next c
        Console.WriteLine()
    End Sub 'Main
End Class 'UTF8EncodingExample

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

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();

        int charCount = utf8.GetCharCount(bytes, 2, 13);
        chars = new Char[charCount];
        int charsDecodedCount = utf8.GetChars(bytes, 2, 13, chars, 0);

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);
        }
        Console.WriteLine();
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   Char chars[];
   Byte bytes[] = 
   {
      85,  84,  70,  56,  32,  69, 110,
      99, 111, 100, 105, 110, 103,  32,
      69, 120,  97, 109, 112, 108, 101
   };

   UTF8Encoding* utf8 = new UTF8Encoding();

   int charCount = utf8 -> GetCharCount(bytes, 2, 13);
   chars = new Char[charCount];
   int charsDecodedCount = utf8 -> GetChars(bytes, 2, 13, chars, 0);

   Console::WriteLine(S"{0} characters used to decode bytes.", __box(charsDecodedCount));

   Console::Write(S"Decoded chars: ");
   IEnumerator* myEnum = chars->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Char* c = __try_cast<Char*>(myEnum->Current);
      Console::Write(S"[{0}]", c -> ToString());
   }
   Console::WriteLine();
}

[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

UTF8Encoding Class | UTF8Encoding Members | System.Text Namespace

Show: