Click here to Skip to main content
15,906,567 members
Home / Discussions / C#
   

C#

 
GeneralRe: Framework 1.1 and 2 Pin
Judah Gabriel Himango16-Sep-05 8:16
sponsorJudah Gabriel Himango16-Sep-05 8:16 
QuestionDataRow array Pin
zaboboa16-Sep-05 5:28
zaboboa16-Sep-05 5:28 
AnswerRe: DataRow array Pin
miah alom16-Sep-05 6:02
miah alom16-Sep-05 6:02 
AnswerRe: DataRow array Pin
Jon Sagara16-Sep-05 6:18
Jon Sagara16-Sep-05 6:18 
QuestionSmart document security Pin
WillemM16-Sep-05 3:47
WillemM16-Sep-05 3:47 
QuestionCalculating dates from tables and displaying results Pin
AdamskiR16-Sep-05 3:02
AdamskiR16-Sep-05 3:02 
QuestionObjects to bytes... Pin
Niklas Ulvinge16-Sep-05 2:47
Niklas Ulvinge16-Sep-05 2:47 
AnswerRe: Objects to bytes... Pin
turbochimp16-Sep-05 17:03
turbochimp16-Sep-05 17:03 
If you're just wanting to serialize the array, the following should work (generically, for any binary serializable type):

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

namespace SomeStuff
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			string[] foo = new string[3]{"a", "b", "c"};
			byte[] bytes = BinaryConverter.SerializeThis(foo);
			
			string[] newFoo = (string[])BinaryConverter.DeserializeThat(bytes);
			foreach(string s in newFoo)
				Console.WriteLine(s);

			Console.ReadLine();
		}
	}

	class BinaryConverter
	{
		private BinaryConverter(){}

		public static object DeserializeThat(byte[] bytes)
		{
			object graph = null;
			if (bytes != null && bytes.Length > 0)
			{
				BinaryFormatter formatter = new BinaryFormatter();
				MemoryStream stream = new MemoryStream(bytes);
				graph = formatter.Deserialize(stream);
				stream.Close();
			}
			return graph;
		}

		public static byte[] SerializeThis(object graph)
		{
			byte[] bytes = null;
			if (graph != null)
			{
				BinaryFormatter formatter = new BinaryFormatter();
				MemoryStream stream = new MemoryStream();
				formatter.Serialize(stream, graph);
				bytes = stream.GetBuffer();
				stream.Close();
			}
			return bytes;
		}
	}
}


The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

GeneralRe: Objects to bytes... Pin
Niklas Ulvinge16-Sep-05 21:07
Niklas Ulvinge16-Sep-05 21:07 
GeneralRe: Objects to bytes... Pin
Niklas Ulvinge16-Sep-05 21:25
Niklas Ulvinge16-Sep-05 21:25 
GeneralRe: Objects to bytes... Pin
turbochimp17-Sep-05 6:25
turbochimp17-Sep-05 6:25 
GeneralRe: Objects to bytes... Pin
Niklas Ulvinge17-Sep-05 6:32
Niklas Ulvinge17-Sep-05 6:32 
GeneralRe: Objects to bytes... Pin
turbochimp17-Sep-05 6:42
turbochimp17-Sep-05 6:42 
GeneralRe: Objects to bytes... Pin
Niklas Ulvinge17-Sep-05 7:00
Niklas Ulvinge17-Sep-05 7:00 
GeneralRe: Objects to bytes... Pin
turbochimp17-Sep-05 6:23
turbochimp17-Sep-05 6:23 
QuestionHow to call ITAddressTranslation method Pin
Fredy16-Sep-05 1:08
Fredy16-Sep-05 1:08 
QuestionC# and voice chat Pin
pakFari16-Sep-05 1:07
pakFari16-Sep-05 1:07 
QuestionDateTime search with Data Tables "Select" Method Pin
16-Sep-05 0:50
suss16-Sep-05 0:50 
Questionimage on toolbarbutton Pin
g00fyman16-Sep-05 0:46
g00fyman16-Sep-05 0:46 
AnswerRe: image on toolbarbutton Pin
Subrahmanyam K16-Sep-05 1:01
Subrahmanyam K16-Sep-05 1:01 
GeneralRe: image on toolbarbutton Pin
g00fyman16-Sep-05 1:07
g00fyman16-Sep-05 1:07 
AnswerRe: image on toolbarbutton Pin
mav.northwind16-Sep-05 5:05
mav.northwind16-Sep-05 5:05 
QuestionThread and timer problem Pin
NicklasRing15-Sep-05 23:41
NicklasRing15-Sep-05 23:41 
AnswerRe: Thread and timer problem Pin
mav.northwind16-Sep-05 21:50
mav.northwind16-Sep-05 21:50 
QuestionCLR Pin
Anonymous15-Sep-05 23:37
Anonymous15-Sep-05 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.