Saturday, May 09, 2015

Convert Byte Array to Memory Stream and vice versa

Convert byte Array to Memory Stream
Stream stream = new MemoryStream(byteArray);

Convert Memory Stream to Byte Array
Byte[] byteArraystream.ToArray();

Convert String to byte Array
static byte[] GetBytes(string str)
{   byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;

Convert Byte Array to String
static string GetString(byte[] bytes)
{   char[] chars = new char[bytes.Length / sizeof(char)];
    System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
    return new string(chars);

Write Byte Array into File
File.WriteAllBytes(string path, byte[] bytes)

Add double quotes in the string:
string str = "\"Add double quotes\"";

No comments: