Solved

Memory stream to Base64

Posted on 2011-09-29
1
6,298 Views
Last Modified: 2012-05-12
How can I do these 3 things
1) Use a memorystream

MemoryStream m = new MemoryStream();

serialize object into stream.

2) Use MemoryStream.ToArray()
which will convert your memorystream into a byte []

3) Call Convert.ToBase64(yourbytes) which will return you a byte []

Thanks in Advance
0
Comment
Question by:ZURINET
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36816891
Perhaps something like this?

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace _27361478
{
    class Program
    {
        static void Main(string[] args)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter serializer = new BinaryFormatter();
            Car c = new Car() { Make = "Honda", Model = "Civic" };

            serializer.Serialize(ms, c);
            byte[] byteArr = ms.ToArray();
            string binStr = Convert.ToBase64String(byteArr);
            byte[] binary = System.Text.Encoding.ASCII.GetBytes(binStr);
        }
    }

    [Serializable()]
    public class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
    }
}

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question