Sdílet prostřednictvím


Encoder.GetBytes Method (array<Char[], Int32, Int32, array<Byte[], Int32, Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When overridden in a derived class, encodes a set of characters from the specified character array and any characters in the internal buffer into the specified byte array. A parameter indicates whether to clear the internal state of the encoder after the conversion.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public MustOverride Function GetBytes ( _
    chars As Char(), _
    charIndex As Integer, _
    charCount As Integer, _
    bytes As Byte(), _
    byteIndex As Integer, _
    flush As Boolean _
) As Integer
public abstract int GetBytes(
    char[] chars,
    int charIndex,
    int charCount,
    byte[] bytes,
    int byteIndex,
    bool flush
)

Parameters

  • chars
    Type: array<System.Char[]
    The character array containing the set of characters to encode.
  • charIndex
    Type: System.Int32
    The zero-based index of the first character to encode.
  • charCount
    Type: System.Int32
    The number of characters to encode.
  • bytes
    Type: array<System.Byte[]
    The byte array to contain the resulting sequence of bytes.
  • byteIndex
    Type: System.Int32
    The zero-based index at which to start writing the resulting sequence of bytes.
  • flush
    Type: System.Boolean
    true to clear the internal state of the encoder after the conversion; otherwise, false.

Return Value

Type: System.Int32
The actual number of bytes written into bytes.

Exceptions

Exception Condition
ArgumentNullException

chars is nulla null reference (Nothing in Visual Basic).

-or-

bytes is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

charIndex or charCount or byteIndex is less than zero.

-or-

charIndex and charCount do not denote a valid range in chars.

-or-

byteIndex is not a valid index in bytes.

ArgumentException

bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes.

EncoderFallbackException

A fallback occurred (see Understanding Encodings for fuller explanation).

Remarks

The Encoder object saves state between calls to GetBytes. When the application is done with a stream of data, it should set the flush parameter to true in the last call to GetBytes to make sure that the state information is flushed and that the encoded bytes are properly terminated. With this setting, the encoder ignores invalid bytes at the end of the data block, such as unmatched surrogates or incomplete combining sequences, and clears the internal buffer.

To calculate the exact buffer size that GetBytes requires to store the resulting characters, call the GetByteCount method.

If GetBytes is called with flush set to false, the encoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next encoding operation. The application should call GetByteCount on a block of data immediately before calling GetBytes on the same block, so that any trailing characters from the previous block are included in the calculation.

Examples

The following example demonstrates how to encode a range of elements from a character array and store the encoded bytes in a range of elements in a byte array. The GetByteCount method is used to determine the size of the array required by GetBytes.

Imports System.Text
Imports Microsoft.VisualBasic.Strings

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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 uniEncoder As Encoder = Encoding.Unicode.GetEncoder()

      Dim byteCount As Integer = _
          uniEncoder.GetByteCount(chars, 0, chars.Length, True)
      bytes = New Byte(byteCount - 1) {}
      Dim bytesEncodedCount As Integer = _
          uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, True)

      outputBlock.Text &= String.Format( _
          "{0} bytes used to encode characters.", _
          bytesEncodedCount _
      ) & vbCrLf

      outputBlock.Text &= "Encoded bytes: "
      Dim b As Byte
      For Each b In bytes
         outputBlock.Text &= String.Format("[{0}]", b)
      Next b
      outputBlock.Text &= vbCrLf
   End Sub 'Main
End Class 'EncoderExample
using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Byte[] bytes;
      // Unicode characters.
      Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

      Encoder uniEncoder = Encoding.Unicode.GetEncoder();

      int byteCount = uniEncoder.GetByteCount(chars, 0, chars.Length, true);
      bytes = new Byte[byteCount];
      int bytesEncodedCount = uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, true);

      outputBlock.Text += String.Format(
          "{0} bytes used to encode characters.", bytesEncodedCount
      ) + "\n";

      outputBlock.Text += "Encoded bytes: ";
      foreach (Byte b in bytes)
      {
         outputBlock.Text += String.Format("[{0}]", b);
      }
      outputBlock.Text += "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.