This documentation is archived and is not being maintained.

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

.NET Framework 1.1

When overridden in a derived class, encodes a range of characters from a character array into a byte array.

[Visual Basic]
Overloads Public MustOverride Function GetBytes( _
   ByVal chars() As Char, _
   ByVal charIndex As Integer, _
   ByVal charCount As Integer, _
   ByVal bytes() As Byte, _
   ByVal byteIndex As Integer _
) As Integer
public abstract int GetBytes(
 char[] chars,
 int charIndex,
 int charCount,
 byte[] bytes,
 int byteIndex
public: virtual int GetBytes(
 __wchar_t chars __gc[],
 int charIndex,
 int charCount,
 unsigned char bytes __gc[],
 int byteIndex
) = 0;
public abstract function GetBytes(
   chars : Char[],
 charIndex : int,
 charCount : int,
 bytes : Byte[],
 byteIndex : int
) : int;


The character array to encode.
The starting index of the character array to encode.
The number of characters to encode.
The byte array where the resulting encoding is stored.
The starting index of the resulting encoding in the byte array.

Return Value

The number of bytes stored in array bytes.


Exception Type Condition
ArgumentException bytes does not contain sufficient space to store the encoded characters.
ArgumentNullException chars or bytes is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException charIndex, charCount or byteIndex is less than zero.


charIndex + charCount is greater than the length of chars.


Encodes a range of characters in a character array into a range of bytes in a byte array. An exception occurs if the byte array is not large enough to hold the complete encoding of the characters.

GetByteCount can be used to determine the exact number of bytes that will be produced for a given range of characters. Alternatively, GetMaxByteCount can be used to determine the maximum number of bytes that will be produced for a given number of characters, regardless of the actual character values.


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

Encoding Class | Encoding Members | System.Text Namespace | Encoding.GetBytes Overload List | GetByteCount | GetMaxByteCount | GetDecoder | GetEncoder