Click here to Skip to main content
15,918,676 members
Home / Discussions / C#
   

C#

 
GeneralSockets/Objects/Serializable Pin
Paddy24-May-02 1:30
Paddy24-May-02 1:30 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson24-May-02 7:54
James T. Johnson24-May-02 7:54 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo24-May-02 19:39
tmagoo24-May-02 19:39 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson25-May-02 4:17
James T. Johnson25-May-02 4:17 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy25-May-02 4:39
Paddy25-May-02 4:39 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo26-May-02 3:35
tmagoo26-May-02 3:35 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy27-May-02 2:37
Paddy27-May-02 2:37 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo29-May-02 10:59
tmagoo29-May-02 10:59 
The GetObjectBytes does not return the bytes as expected. Consider the following code.

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

namespace CBinFormatter
{
///
/// Summary description for Class1.
///

class CBinFormatter
{
[Serializable()]
public struct sValues
{
public int iFirst;
public int iSecond;
public int iThird;
}

///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
sValues sVal = new sValues();

sVal.iFirst = 100;
sVal.iSecond = 300;
sVal.iThird = 400;

byte [] btVal = GetObjectBytes(sVal);

Console.WriteLine("The size of the btVal is " + btVal.Length);
}

static byte[] GetObjectBytes(object o)
{
MemoryStream memstream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(memstream, o);
memstream.Close(); // Prevent further writing
return memstream.GetBuffer();
}

static object GetObjectFromBytes(byte [] bytes)
{
MemoryStream memstream = new MemoryStream(bytes);
IFormatter formatter = new BinaryFormatter();
object o = formatter.Deserialize(memstream);
memstream.Close(); return o;
}
}
}


The Binary Serializer will return a size which is greater than the three int variables from the structure. Plus, when looking at the byte values within the debugger, I can't find the 100, 300, 400. I would of expected that the bytes returned would of been the size of 12 bytes " 3 int values - 4 bytes each". Do you know what I am missing here?

Tom McDaniel
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson29-May-02 18:02
James T. Johnson29-May-02 18:02 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo30-May-02 3:14
tmagoo30-May-02 3:14 
GeneralPlatform Interop - Structures and Arrays Pin
Mikko Puonti24-May-02 0:09
Mikko Puonti24-May-02 0:09 
GeneralXmlDocument with DTD driving me insane! Pin
Daniel Turini23-May-02 10:21
Daniel Turini23-May-02 10:21 
QuestionWindows services???? Pin
Rickard Andersson2023-May-02 4:37
Rickard Andersson2023-May-02 4:37 
AnswerRe: Windows services???? Pin
Nish Nishant23-May-02 17:06
sitebuilderNish Nishant23-May-02 17:06 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 20:48
Rickard Andersson2023-May-02 20:48 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 20:52
sitebuilderNish Nishant23-May-02 20:52 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 21:20
Rickard Andersson2023-May-02 21:20 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 21:20
sitebuilderNish Nishant23-May-02 21:20 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 22:17
Rickard Andersson2023-May-02 22:17 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 22:17
sitebuilderNish Nishant23-May-02 22:17 
GeneralRe: Windows services???? Pin
A.A.29-May-02 17:20
A.A.29-May-02 17:20 
QuestionNotifyIcon and ContextMenu Bug ?? Pin
2sky23-May-02 1:28
2sky23-May-02 1:28 
AnswerRe: NotifyIcon and ContextMenu Bug ?? Pin
Nish Nishant23-May-02 1:34
sitebuilderNish Nishant23-May-02 1:34 
GeneralRe: NotifyIcon and ContextMenu Bug ?? Pin
23-May-02 1:57
suss23-May-02 1:57 
GeneralRe: NotifyIcon and ContextMenu Bug ?? Pin
2sky23-May-02 2:05
2sky23-May-02 2:05 

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.