Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

UTF7Encoding.GetBytes Method

Encodes a set of elements from a Unicode character array, and stores the results in a set of elements in a byte array.

Overload List

Encodes a specified range of elements from a Unicode character array, and stores the results in 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;

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(String) As Byte()
[C#] public virtual byte[] GetBytes(string);
[C++] public: virtual unsigned char GetBytes(String*) __gc[];
[JScript] public function GetBytes(String) : 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[];

Inherited from Encoding.

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetBytes method to encode a range of elements from the Unicode character array 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 elements.

[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
Imports Microsoft.VisualBasic.strings

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        Dim bytes() As Byte
        ' Unicode characters.
        ' ChrW(35)  = #
        ' ChrW(37)  = %
        ' ChrW(928) = Pi
        ' ChrW(931) = Sigma
        Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
        
        Dim utf7 As New UTF7Encoding()
        
        Dim byteCount As Integer = utf7.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = utf7.GetBytes(chars, 1, 2, bytes, 0)
        
        Console.WriteLine("{0} bytes used to encode characters.", 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 'UTF7EncodingExample

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

class UTF7EncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UTF7Encoding utf7 = new UTF7Encoding();
        
        int byteCount = utf7.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = utf7.GetBytes(chars, 1, 2, bytes, 0);
        
        Console.WriteLine(
            "{0} bytes used to encode characters.", 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[];
   // Unicode characters.
   Char chars[] = 
   {
      L'\u03a0', // Pi
      L'\u03a3', // Sigma
      L'\u03a6',
      L'\u03a9'
   };

   UTF7Encoding* utf7 = new UTF7Encoding();

   int byteCount = utf7 -> GetByteCount(chars, 1, 2);
   bytes = new Byte[byteCount];
   int bytesEncodedCount = utf7 -> GetBytes(chars, 1, 2, bytes, 0);

   Console::WriteLine(S"{0} bytes used to encode characters.", __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

UTF7Encoding Class | UTF7Encoding Members | System.Text Namespace

Show:
© 2015 Microsoft