Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

UnicodeEncoding.GetBytes Method (Char[], Int32, Int32, Byte[], Int32)

.NET Framework 1.1

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.

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

Parameters

chars
The character array to encode.
charIndex
The index of the first character in chars to encode.
charCount
The number of characters to encode.
bytes
The byte array where the encoded results are stored.
byteIndex
The index of the first element in bytes where the encoded results are stored.

Return Value

The number of bytes stored in bytes.

Exceptions

Exception Type Condition
ArgumentNullException chars or bytes is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException charIndex, charCount or byteIndex is less than zero.

-or-

The sum of charIndex and charCount is greater than the length of chars.

-or-

byteIndex is greater than the length of bytes.

ArgumentException bytes does not contain sufficient space to store the encoded characters.

Remarks

Use GetByteCount to calculate exactly, or GetMaxByteCount to calculate at most, the array size required by the GetBytes method to store encoded characters.

Example

[Visual Basic, C#, C++] The following example demonstrates how 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] 
Imports System
Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class UnicodeEncodingExample
    
    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 uni As New UnicodeEncoding()
        
        Dim byteCount As Integer = uni.GetByteCount(chars, 1, 2)
        bytes = New Byte(byteCount - 1) {}
        Dim bytesEncodedCount As Integer = uni.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 'UnicodeEncodingExample

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

class UnicodeEncodingExample {
    public static void Main() {
        Byte[] bytes;
        // Unicode characters.
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };
        
        UnicodeEncoding Unicode = new UnicodeEncoding();
        
        int byteCount = Unicode.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = Unicode.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'
   };

   UnicodeEncoding* Unicode = new UnicodeEncoding();

   int byteCount = Unicode -> GetByteCount(chars, 1, 2);
   bytes = new Byte[byteCount];
   int bytesEncodedCount = Unicode -> 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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace | UnicodeEncoding.GetBytes Overload List | GetByteCount | GetMaxByteCount

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.