This documentation is archived and is not being maintained.

UnicodeEncoding.GetBytes Method

Encodes a specified range of characters from a Unicode character array or a String and stores the results into a specified byte array.

Overload List

Encodes the characters from a specified String and returns the results in a byte array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetBytes(String) As Byte()
[C#] public override byte[] GetBytes(string);
[C++] public: unsigned char GetBytes(String*) __gc[];
[JScript] public override function GetBytes(String) : Byte[];

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

Supported by the .NET Compact Framework.

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

Encodes a specified range of characters from a String and stores the results into a specified range of elements in a byte array.

Supported by the .NET Compact Framework.

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

Inherited from Encoding.

Supported by the .NET Compact Framework.

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

Inherited from Encoding.

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetBytes method to encode a range of characters from the String chars and store the encoded bytes in a range of elements in the byte array bytes. The GetByteCount method is used to determine the number of bytes required to store the encoded characters.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetBytes. 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 bytes() As Byte
        Dim chars As String = "Unicode Encoding Example"
        
        Dim uni As New UnicodeEncoding()
        
        Dim byteCount As Integer = uni.GetByteCount(chars.ToCharArray(), 8, 8)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = uni.GetBytes(chars, 8, 8, bytes, 0)
        
        Console.WriteLine("{0} bytes used to encode string.", bytesEncodedCount)
        
        Console.Write("Encoded bytes: ")
        Dim b As Byte
        For Each b In  bytes
            Console.Write("[{0}]", b)
        Next b
        Console.WriteLine()
    End Sub 'Main
End Class 'UnicodeEncodingExample

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

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes;
        String chars = "Unicode Encoding Example";
        
        UnicodeEncoding Unicode = new UnicodeEncoding();
        
        int byteCount = Unicode.GetByteCount(chars.ToCharArray(), 8, 8);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = Unicode.GetBytes(chars, 8, 8, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode string.", bytesEncodedCount
        );

        Console.Write("Encoded bytes: ");
        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main()
{
   Byte bytes[];
   String * chars = S"Unicode Encoding Example";

   UnicodeEncoding* Unicode = new UnicodeEncoding();

   int byteCount = Unicode -> GetByteCount(chars -> ToCharArray(), 8, 8);
   bytes = new Byte[byteCount];
   int bytesEncodedCount = Unicode -> GetBytes(chars, 8, 8, bytes, 0);

   Console::WriteLine(S"{0} bytes used to encode string.", __box(bytesEncodedCount));

   Console::Write(S"Encoded bytes: ");
   IEnumerator* myEnum = bytes->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Byte b = *__try_cast<Byte __gc*>(myEnum->Current);
      Console::Write(S"[{0}]", __box(b));
   }
   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: