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

CSharp examples for System:Byte Array

Description

Converts array of characters into array of bytes, using specified encoding.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  w w  w.  j  av a  2 s.co m*/

public class Main{
        /// <summary>
    ///   <para>Converts array of characters into array of bytes, using specified encoding.</para>
    /// </summary>
    /// <param name="self">Source array of characters.</param>
    /// <param name="encoding">Encoding to be used for transforming between <see cref="char"/> at its <see cref="byte"/> equivalent. If not specified, uses <see cref="Encoding.UTF8"/> encoding.</param>
    /// <returns>Array of bytes 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="Encoding.GetBytes(char[])"/>
    public static byte[] Bytes(this char[] self, Encoding encoding = null)
    {
      Assertion.NotNull(self);

      return (encoding ?? Encoding.UTF8).GetBytes(self);
    }
}

Related Tutorials