String extension method which converts the input string to a byte array. - CSharp System

CSharp examples for System:Byte

Description

String extension method which converts the input string to a byte array.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from   w ww .j  a va 2  s.  c  o m

public class Main{
        /// <summary>
        /// String extension method which converts the input string to a byte array.
        /// </summary>
        /// <param name="input">The string on which the method is invoked.</param>
        /// <returns>Returns a byte array of the input string</returns>
        public static byte[] ToByteArray(this string input)
        {
            var bytesArray = new byte[input.Length * sizeof(char)];
            Buffer.BlockCopy(input.ToCharArray(), 0, bytesArray, 0, bytesArray.Length);
            return bytesArray;
        }
}

Related Tutorials