Encoding.GetString Method

Definition

When overridden in a derived class, decodes a sequence of bytes into a string.

Overloads

GetString(Byte[])

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

GetString(ReadOnlySpan<Byte>)

When overridden in a derived class, decodes all the bytes in the specified byte span into a string.

GetString(Byte*, Int32)

When overridden in a derived class, decodes a specified number of bytes starting at a specified address into a string.

GetString(Byte[], Int32, Int32)

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

GetString(Byte[])

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

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

C#
public virtual string GetString (byte[] bytes);

Parameters

bytes
Byte[]

The byte array containing the sequence of bytes to decode.

Returns

A string that contains the results of decoding the specified sequence of bytes.

Exceptions

The byte array contains invalid Unicode code points.

bytes is null.

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example reads a UTF-8 encoded string from a binary file represented by a FileStream object. For files that are smaller than 2,048 bytes, it reads the contents of the entire file into a byte array and calls the GetString(Byte[]) method to perform the decoding. For larger files, it reads 2,048 bytes at a time into a byte array, calls the Decoder.GetCharCount(Byte[], Int32, Int32) method to determine how many characters are contained in the array, and then calls the Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) method to perform the decoding.

C#
using System;
using System.IO;
using System.Text;

public class Example
{
   const int MAX_BUFFER_SIZE = 2048;
   static Encoding enc8 = Encoding.UTF8;

   public static void Main()
   {
      FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
      string contents = null;
      
      // If file size is small, read in a single operation.
      if (fStream.Length <= MAX_BUFFER_SIZE) {
         Byte[] bytes = new Byte[fStream.Length];
         fStream.Read(bytes, 0, bytes.Length);
         contents = enc8.GetString(bytes);
      }
      // If file size exceeds buffer size, perform multiple reads.
      else {
         contents = ReadFromBuffer(fStream);
      }
      fStream.Close();
      Console.WriteLine(contents);
   }

   private static string ReadFromBuffer(FileStream fStream)
   {
        Byte[] bytes = new Byte[MAX_BUFFER_SIZE];
        string output = String.Empty;
        Decoder decoder8 = enc8.GetDecoder();
      
        while (fStream.Position < fStream.Length) {
           int nBytes = fStream.Read(bytes, 0, bytes.Length);
           int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
           char[] chars = new char[nChars];
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
           output += new String(chars, 0, nChars);                                                     
        }
        return output;
    }
}
// The example displays the following output:
//     This is a UTF-8-encoded file that contains primarily Latin text, although it
//     does list the first twelve letters of the Russian (Cyrillic) alphabet:
//     
//     А б в г д е ё ж з и й к
//     
//     The goal is to save this file, then open and decode it as a binary stream.

The example uses the following text, which should be saved to a UTF-8 encoded file named Utf8Example.txt.

txt
This is a UTF-8-encoded file that contains primarily Latin text, although it
does list the first twelve letters of the Russian (Cyrillic) alphabet:

А б в г д е ё ж з и й к

The goal is to save this file, then open and decode it as a binary stream.

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, you should use the Decoder object returned by the GetDecoder method of a derived class.

See the Remarks section of the Encoding.GetChars reference topic for a discussion of decoding techniques and considerations.

Note that the precise behavior of the GetString method for a particular Encoding implementation depends on the fallback strategy defined for that Encoding object. For more information, see the "Choosing a Fallback Strategy" section of the Character Encoding in .NET topic.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetString(ReadOnlySpan<Byte>)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

When overridden in a derived class, decodes all the bytes in the specified byte span into a string.

C#
public string GetString (ReadOnlySpan<byte> bytes);

Parameters

bytes
ReadOnlySpan<Byte>

A read-only byte span to decode to a Unicode string.

Returns

A string that contains the decoded bytes from the provided read-only span.

Remarks

The GetString method is designed to optimize performance. Instead of creating a managed byte array and then decoding it, you can instead call this method without having to create any intermediate objects.

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, you should use the Decoder object returned by the GetDecoder method of a derived class.

See the Remarks section of the Encoding.GetChars reference topic for a discussion of decoding techniques and considerations.

Note that the precise behavior of the GetString method for a particular Encoding implementation depends on the fallback strategy defined for that Encoding object. For more information, see the "Choosing a Fallback Strategy" section of the Character Encoding in .NET topic.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

GetString(Byte*, Int32)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

Important

This API is not CLS-compliant.

When overridden in a derived class, decodes a specified number of bytes starting at a specified address into a string.

C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public string GetString (byte* bytes, int byteCount);
C#
[System.CLSCompliant(false)]
public string GetString (byte* bytes, int byteCount);
C#
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(false)]
public string GetString (byte* bytes, int byteCount);

Parameters

bytes
Byte*

A pointer to a byte array.

byteCount
Int32

The number of bytes to decode.

Returns

A string that contains the results of decoding the specified sequence of bytes.

Attributes

Exceptions

bytes is a null pointer.

byteCount is less than zero.

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

-and-

DecoderFallback is set to DecoderExceptionFallback.

Remarks

The GetString method is designed to optimize performance when you have a native pointer to a byte array. Instead of creating a managed byte array and then decoding it, you can instead call this method without having to create any intermediate objects.

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, you should use the Decoder object returned by the GetDecoder method of a derived class.

See the Remarks section of the Encoding.GetChars reference topic for a discussion of decoding techniques and considerations.

Note that the precise behavior of the GetString method for a particular Encoding implementation depends on the fallback strategy defined for that Encoding object. For more information, see the "Choosing a Fallback Strategy" section of the Character Encoding in .NET topic.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetString(Byte[], Int32, Int32)

Source:
Encoding.cs
Source:
Encoding.cs
Source:
Encoding.cs

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

C#
public virtual string GetString (byte[] bytes, int index, int count);

Parameters

bytes
Byte[]

The byte array containing the sequence of bytes to decode.

index
Int32

The index of the first byte to decode.

count
Int32

The number of bytes to decode.

Returns

A string that contains the results of decoding the specified sequence of bytes.

Exceptions

The byte array contains invalid Unicode code points.

bytes is null.

index or count is less than zero.

-or-

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

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

Examples

The following example reads a UTF-8 encoded string from a binary file that is represented by a FileStream object. For files that are smaller than 2,048 bytes, it reads the contents of the entire file into a byte array and calls the GetString(Byte[], Int32, Int32) method to perform the decoding. For larger files, it reads 2,048 bytes at a time into a byte array, calls the Decoder.GetCharCount(Byte[], Int32, Int32) method to determine how many characters are contained in the array, and then calls the Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32) method to perform the decoding.

C#
using System;
using System.IO;
using System.Text;

public class Example
{
   const int MAX_BUFFER_SIZE = 2048;
   static Encoding enc8 = Encoding.UTF8;
   static byte[] bytes = new byte[MAX_BUFFER_SIZE]; 

   public static void Main()
   {
      FileStream fStream = new FileStream(@".\Utf8Example.txt", FileMode.Open);
      string contents = null;
      
      // If file size is small, read in a single operation.
      if (fStream.Length <= MAX_BUFFER_SIZE) {
         int bytesRead = fStream.Read(bytes, 0, bytes.Length);
         contents = enc8.GetString(bytes, 0, bytesRead);
      }   
      // If file size exceeds buffer size, perform multiple reads.
      else {
         contents = ReadFromBuffer(fStream);
      }
      fStream.Close();
      Console.WriteLine(contents);
   }

    private static string ReadFromBuffer(FileStream fStream)
    {
        string output = String.Empty;
        Decoder decoder8 = enc8.GetDecoder();
      
        while (fStream.Position < fStream.Length) {
           int nBytes = fStream.Read(bytes, 0, bytes.Length);
           int nChars = decoder8.GetCharCount(bytes, 0, nBytes);
           char[] chars = new char[nChars];
           nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0);
           output += new String(chars, 0, nChars);                                                     
        }
        return output;
    }   
}
// The example displays the following output:
//     This is a UTF-8-encoded file that contains primarily Latin text, although it
//     does list the first twelve letters of the Russian (Cyrillic) alphabet:
//     
//     А б в г д е ё ж з и й к
//     
//     The goal is to save this file, then open and decode it as a binary stream.

The example uses the following text, which should be saved to a UTF-8 encoded file named Utf8Example.txt.

txt
This is a UTF-8-encoded file that contains primarily Latin text, although it
does list the first twelve letters of the Russian (Cyrillic) alphabet:

А б в г д е ё ж з и й к

The goal is to save this file, then open and decode it as a binary stream.

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, you should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.

See the Remarks section of the Encoding.GetChars reference topic for a discussion of decoding techniques and considerations.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0