I have done some researches on this topic and found some solutions with the help of
- MemoryStream and BinaryFormatter classes
- Marshal class
But neither of these methods work for my class because my classes have an array.
Here is the test class I am working with:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public class ByteArrayInClass
{
private byte _option;
private ushort _nElements;
private byte[] arrayElements;
public ByteArrayInClass(byte option, ushort nElements)
{
this._option = option;
this._nElements = nElements;
arrayElements = new byte[nElements];
for (int i = 0; i < arrayElements.Length; i++)
{
arrayElements[i] = (byte)i;
}
}
public static byte[] ObjectToBytes(byteArrayInClass value)
{
}
public static byteArrayInClass BytesToObject(byte[] bytes)
{
}
}
In my main:
testObject1 = new ByteArrayInClass(3, 10);
byte[] testBytes1 = ByteArrayInClass.ObjectToBytes(testObject1);
byte[] testBytes2 = { 3, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ByteArrayInClass testObject2 = ByteArrayInClass.BytesToObjectbyte(testBytes2);
I am starting to think that I need to convert the members of the class to bytes one by one, and vice versa to convert bytes to object. Could someone point me to the right direction?
Edit: I was not clear enough on what I am trying to do. I am working on a program which communicates with a server. It involves receiving data and sending data. The data are sent and received in bytes, and when I receive bytes of data, I need to construct a class with the received bytes, so I understand what is being sent to me. When I send data to the server, I first construct a class with the appropriate values, then convert the object to bytes, so I can send the data to the server. Hopefully this explain a little better on what I am trying to do.
It seems like there isn't a easy way to convert a class to bytes, so I am converting each class members to bytes myself. So the following is what I come up with. Please feel free to let me know if this is a dumb way to accomplish the task. I would like to learn a smarter way to do it.
public static int GetObjectSize(ByteArrayInClass value)
{
return Marshal.SizeOf(value.Option) + Marshal.SizeOf(value.ElementCount) + (value.ElementCount * 1);
}
public static byte[] ObjectToBytes(ByteArrayInClass value)
{
int copyIndex = 0;
byte[] resultBytes = new byte[GetObjectSize(value)];
resultBytes[copyIndex] = value.Option;
copyIndex += 1;
byte[] elementCountBytes = BitConverter.GetBytes(value.ElementCount);
elementCountBytes.CopyTo(resultBytes, copyIndex);
copyIndex += elementCountBytes.Length;
value.ElementArray.CopyTo(resultBytes, copyIndex);
return resultBytes;
}
public static ByteArrayInClass BytesTObject(byte[] bytes)
{
int convertIndex = 0;
byte option = bytes[convertIndex];
convertIndex += 1;
ushort elementCount = BitConverter.ToUInt16(bytes, convertIndex);
convertIndex += 2;
ByteArrayInClass resultObj = new ByteArrayInClass(option, elementCount);
byte[] elementArray = new byte[elementCount];
int j = 0;
for (int i = convertIndex; i < (convertIndex + elementCount); i++)
{
elementArray[j++] = bytes[i];
}
resultObj.ElementArray = elementArray;
return resultObj;
}
StructLayoutattribute and have you tried serializing and des-erializing without it? Normally objects like this are marked with the[Serializable]attribute. See here: msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.110).aspx