Convert a string into an array of bytes : Memory Stream « File Stream « C# / C Sharp






Convert a string into an array of bytes

   

using System;
using System.IO;
using System.Text;

    public static class CompressionUtility
    {
        /// <summary>
        /// Convert a string into an array of bytes
        /// </summary>
        /// <param name="input">String to convert</param>
        /// <returns>Byte atray that repsesents the string</returns>
        public static byte[] ConvertStringToBytes(string input)
        {
            MemoryStream stream = new MemoryStream();
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.Write(input);
                writer.Flush();
            }
            return stream.ToArray();
        }

    }

   
    
    
  








Related examples in the same category

1.Create a MemoryStream
2.MemoryStream.Write
3.Use MemoryStream and BinaryWriter to convert decimal to byte arrayUse MemoryStream and BinaryWriter to convert decimal to byte array
4.Use MemoryStream and BinaryReader to convert Byte array to decimalUse MemoryStream and BinaryReader to convert Byte array to decimal
5.MemoryStream.ToArray, MemoryStream.ReadDecimal
6.Deep Copy with MemoryStream
7.Memory-Mapped Files
8.Deep clone with MemoryStream
9.Using MemoryStream to Serialize and Desirialize