Converts array of bytes into a string, using specified encoding. - CSharp System

CSharp examples for System:Byte Array

Description

Converts array of bytes into a string, using specified encoding.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w ww  . ja va  2 s .  com*/

public class Main{
        /// <summary>
    ///   <para>Converts array of bytes into a string, using specified encoding.</para>
    /// </summary>
    /// <param name="self">Source array of bytes.</param>
    /// <param name="encoding">Encoding to be used for transforming between <see cref="byte"/> at its <see cref="char"/> equivalent. If not specified, uses <see cref="Encoding.UTF8"/> encoding.</param>
    /// <returns>Array of characters as a string which represents <paramref name="self"/> array in <paramref name="encoding"/>.</returns>
    /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="String(char[])"/>
    /// <seealso cref="Encoding.GetString(byte[], int, int)"/>
    public static string String(this byte[] self, Encoding encoding = null)
    {
      Assertion.NotNull(self);

      return (encoding ?? Encoding.UTF8).GetString(self, 0, self.Length);
    }
        /// <summary>
    ///   <para>Returns string representation of specified array of characters.</para>
    /// </summary>
    /// <param name="self">Source array of characters.</param>
    /// <returns>String which is formed from contents of <paramref name="self"/> array.</returns>
    /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
    /// <seealso cref="String(byte[], Encoding)"/>
    public static string String(this char[] self)
    {
      Assertion.NotNull(self);

      return new string(self);
    }
}

Related Tutorials