GetChars Method

UnicodeEncoding.GetChars Method

Decodes the elements from a specified byte array and stores them as elements in a specified Unicode character array.

Overload List

Decodes a range of elements from a specified byte array and stores them as elements in a specified 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 UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = {85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0}
        
        Dim uni As New UnicodeEncoding()
        
        Dim charCount As Integer = uni.GetCharCount(bytes, 2, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = uni.GetChars(bytes, 2, 8, 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 'UnicodeEncodingExample

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

class UnicodeEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        UnicodeEncoding Unicode = new UnicodeEncoding();

        int charCount = Unicode.GetCharCount(bytes, 2, 8);
        chars = new Char[charCount];
        int charsDecodedCount = Unicode.GetChars(bytes, 2, 8, 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, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
   };

   UnicodeEncoding* Unicode = new UnicodeEncoding();

   int charCount = Unicode -> GetCharCount(bytes, 2, 8);
   chars = new Char[charCount];
   int charsDecodedCount = Unicode -> GetChars(bytes, 2, 8, 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

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace

Show:
© 2016 Microsoft