Encoding.GetString Method

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

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a string.

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

Syntax

'Declaration
Public Overridable Function GetString ( _
    bytes As Byte(), _
    index As Integer, _
    count As Integer _
) As String
public virtual string GetString(
    byte[] bytes,
    int index,
    int count
)

Parameters

  • bytes
    Type: array<System.Byte[]
    The byte array containing the sequence of bytes to decode.
  • index
    Type: System.Int32
    The zero-based index of the first byte to decode.

Return Value

Type: System.String
A String containing the results of decoding the specified sequence of bytes.

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

DecoderFallbackException

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

Remarks

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.

Examples

The following example uses the GetString method to decode an array of UTF16-encoded bytes.

' Declare Byte array in little endian order.
Dim bytes() As Byte = { &h14, &h04, &h3e, &h04, &h31, &h04, &h40, &h04, _ 
                        &h4b, &h04, &h39, &h04, &h20, &h00, &h34, &h04, _ 
                        &h35, &h04, &h3d, &h04, &h21, &h00 }
Dim enc As Encoding = Encoding.GetEncoding("utf-16")
Dim output As String = enc.GetString(bytes, 0, bytes.Length)
outputBlock.Text += output + vbCrLf
' The example displays the following output:
'       Добрый ден!      
// Declare Byte array in little endian order.
Byte[] bytes = { 0x14, 0x04, 0x3e, 0x04, 0x31, 0x04, 0x40, 0x04, 
                 0x4b, 0x04, 0x39, 0x04, 0x20, 0x00, 0x34, 0x04, 
                 0x35, 0x04, 0x3d, 0x04, 0x21, 0x00};
Encoding enc = Encoding.GetEncoding("utf-16");
string output = enc.GetString(bytes, 0, bytes.Length);
outputBlock.Text += output + "\n";
// The example displays the following output:
//       Добрый ден!      

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.