This documentation is archived and is not being maintained.

Encoding.GetString Method (Byte[], Int32, Int32)

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)

public virtual string GetString (
	byte[] bytes,
	int index,
	int count
)
public String GetString (
	byte[] bytes, 
	int index, 
	int count
)
public function GetString (
	bytes : byte[], 
	index : int, 
	count : int
) : String
Not applicable.

Parameters

bytes

The byte array containing the sequence of bytes to decode.

index

The index of the first byte to decode.

count

The number of bytes to decode.

Return Value

A String containing the results of decoding the specified sequence of bytes.

Exception typeCondition

ArgumentNullException

bytes is a 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)

-and-

DecoderFallback is set to DecoderExceptionFallback.

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.

The following code example demonstrates how to read a UTF-8 encoded string from a binary file.

private string ReadAuthor(Stream binary_file) {
  System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  // Read string from binary file with UTF8 encoding
  byte[] buffer = new byte[30];
  binary_file.Read(buffer, 0, 30);
  return encoding.GetString(buffer);
}

private String ReadAuthor(Stream binary_file)
{
    System.Text.Encoding encoding = System.Text.Encoding.get_UTF8();

    // Read string from binary file with UTF8 encoding
    ubyte buffer[] = new ubyte[30];
    binary_file.Read(buffer, 0, 30);
    return encoding.GetString(buffer);
} //ReadAuthor

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: