Export (0) Print
Expand All
3 out of 3 rated this helpful - Rate this topic

Encoding.GetString Method (Byte[])

Updated: March 2012

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

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)
public virtual string GetString(
	byte[] bytes
)

Parameters

bytes
Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

Return Value

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

bytes is null.

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 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.

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.

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

March 2012

Replaced the example.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.