Export (0) Print
Expand All

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

.NET Framework 1.1

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

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

Parameters

s
The string of characters to encode.
charIndex
The index of the first character in s 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 s 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 s.

-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 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] 
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.

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

Show:
© 2014 Microsoft