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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 (Nothing).


index or count is less than zero.


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


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


DecoderFallback is set to DecoderExceptionFallback.

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, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.


UTF7Encoding does not provide error detection. When invalid bytes are encountered, UTF7Encoding generally emits the invalid bytes. If a byte is larger than hexadecimal 0x7F, the byte value is zero-extended into a Unicode character, the result is stored in the chars array, and any shift sequence is terminated. For example, if the byte to encode is hexadecimal 0x81, the resulting character is U+0081. For security reasons, your applications are recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection.

The following code example encodes a string into an array of bytes, and then decodes the bytes back into a string.

using System;
using System.Text;

public class SamplesUTF7Encoding  {

   public static void Main()  {

      // Create an instance of UTF7Encoding.
      UTF7Encoding u7 = new UTF7Encoding( true );

      // Create byte arrays from the same string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string.
      byte[] myBArr = new byte[u7.GetByteCount( myStr )];
      u7.GetBytes( myStr, 0, myStr.Length, myBArr, 0 );

      // Decode the byte array.
      Console.WriteLine( "The new string is: {0}", u7.GetString( myBArr, 0, myBArr.Length ) );



This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The new string is: za??


Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top