C# Encoding GetString(Byte[])

Description

Encoding GetString(Byte[]) When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

Syntax

Encoding.GetString(Byte[]) has the following syntax.


public virtual string GetString(
  byte[] bytes
)

Parameters

Encoding.GetString(Byte[]) has the following parameters.

  • bytes - The byte array containing the sequence of bytes to decode.

Returns

Encoding.GetString(Byte[]) method returns A string that contains the results of decoding the specified sequence of bytes.

Example

The following example reads a UTF-8 encoded string from a binary file represented by a FileStream object.


using System;//from   w ww. j  a v a  2  s .  c  o  m
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;
      Byte[] bytes = new Byte[fStream.Length];
      fStream.Read(bytes, 0, bytes.Length);
      contents = enc8.GetString(bytes);
      fStream.Close();
      Console.WriteLine(contents);
   }
}




















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding