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


Decodes a range of bytes from a byte array into a string.

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

public override string GetString(
	byte[] bytes,
	int index,
	int count


Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

Type: System.Int32

The index of the first byte to decode.

Type: System.Int32

The number of bytes to decode.

Return Value

Type: System.String

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

Exception Condition

bytes is null.


index or count is less than zero.


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


Error detection is enabled, and bytes contains an invalid sequence of bytes.


A fallback occurred (see Character Encoding in the .NET Framework for complete explanation)


DecoderFallback is set to DecoderExceptionFallback.

With error detection, an invalid sequence causes this method to throw an ArgumentException exception. Without error detection, invalid sequences are ignored, and no exception is thrown.

If the range of bytes to be decoded includes the byte order mark (BOM) and the byte array was returned by a method of a non-BOM aware type, the character U+FFFE is included in the character array returned by this method. You can remove it by calling the String.TrimStart method.

Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

The following example initializes an array by calling the GetByteCount method to determine exactly how many bytes are required for an encoded string and then adding the size of the byte order mark (BOM). The example then calls the GetPreamble method to store the BOM to the array before calling the GetBytes method to store the encoded bytes to the array. The example then calls the GetString method to decode the string.

using System;
using System.Text;

public class Example
   public static void Main()
      UTF8Encoding utf8 = new UTF8Encoding(true, true);

      String s = "It was the best of times, it was the worst of times...";

      // We need to dimension the array, since we'll populate it with 2 method calls.
      Byte[] bytes = new Byte[utf8.GetByteCount(s) + utf8.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length);
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf8.GetString(bytes, 0, bytes.Length);
// The example displays the following output:
//        ?It was the best of times, it was the worst of times...

Note that in this case the decoded string differs from the original string, since it begins with a 16-bit byte order mark U+FFFD. This means that the two strings will compare as unequal, and that if the string is output, the BOM will be displayed as the replacement character "?". To remove the BOM at the beginning of the string, you can call the String.TrimStart method.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top