How to convert to byte[]?

  • Thread starter Thread starter Olav Tollefsen
  • Start date Start date
O

Olav Tollefsen

I have a class defines like this:

public enum MessageType : int
{
AddSubscription = 0,
RemoveSubscription = 1,
DistributeMessage = 2
}

public class Message
{
public MessageType messageType;

public string messageName;

public byte[] body;

public Message()
{
}
}

Given that I have an instance of this class, how can I convert it to byte[]
and vice versa?

I need to pass it to Socket.Send and Socket.Receive.
 
"convert IT to a byte[]" - what is "it"? if you are talking about body -
that is already a byte array.. what do you mean??
 
I want to be able to convert an instance of the class Message to an array of
bytes.

What is it?

It's first a MessageType (an int) of 4 bytes, then a string of x bytes, then
a byte array of y bytes.

Together that should be a byte array of 4+x+y bytes that I would like to
send over a Socket to another process and then convert it back to an
instance of the class.

Maybe it would be better to use some kind of serializer???

Olav

Drebin said:
"convert IT to a byte[]" - what is "it"? if you are talking about body -
that is already a byte array.. what do you mean??


Olav Tollefsen said:
I have a class defines like this:

public enum MessageType : int
{
AddSubscription = 0,
RemoveSubscription = 1,
DistributeMessage = 2
}

public class Message
{
public MessageType messageType;

public string messageName;

public byte[] body;

public Message()
{
}
}

Given that I have an instance of this class, how can I convert it to byte[]
and vice versa?

I need to pass it to Socket.Send and Socket.Receive.
 
Drebin said:
"convert IT to a byte[]" - what is "it"? if you are talking about body -
that is already a byte array.. what do you mean??

The instance of the class, presumably.

I would suggest adding something like:

public byte[] ToByteArray()
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter (stream))
{
writer.Write ((int)messageType);
writer.Write (messageName);
writer.Write (body.Length);
writer.Write (body);
}
stream.Flush();
return stream.ToArray();
}
}

and

public Message FromByteArray (byte[] data)
{
using (MemoryStream stream = new MemoryStream (data))
{
using (BinaryReader reader = new BinaryReader (stream))
{
Message ret = new Message();
ret.messageType = (MessageType) reader.ReadInt32();
ret.messageName = reader.ReadString();
int count = reader.ReadInt32();
ret.body = reader.ReadBytes (count);
return ret;
}
}
}

You could put more error checking in if you want - or you could use
serialization, in which case look it up (as serialization) in the MSDN
index for more information.
 
Thanks! Exactly what I was looking for!

Olav

Jon Skeet said:
Drebin said:
"convert IT to a byte[]" - what is "it"? if you are talking about body -
that is already a byte array.. what do you mean??

The instance of the class, presumably.

I would suggest adding something like:

public byte[] ToByteArray()
{
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter (stream))
{
writer.Write ((int)messageType);
writer.Write (messageName);
writer.Write (body.Length);
writer.Write (body);
}
stream.Flush();
return stream.ToArray();
}
}

and

public Message FromByteArray (byte[] data)
{
using (MemoryStream stream = new MemoryStream (data))
{
using (BinaryReader reader = new BinaryReader (stream))
{
Message ret = new Message();
ret.messageType = (MessageType) reader.ReadInt32();
ret.messageName = reader.ReadString();
int count = reader.ReadInt32();
ret.body = reader.ReadBytes (count);
return ret;
}
}
}

You could put more error checking in if you want - or you could use
serialization, in which case look it up (as serialization) in the MSDN
index for more information.
 
Back
Top