Yes, you are looking for serialization.
reference this namespace.
using System.Runtime.Serialization.Formatters.Binary;
Make your test struct serializable, like this.
[Serializable]
public struct test
{
public byte itemcode;
public string itemname;
}
You can now serialize or deserialize by using methods like these.
Serialize returns your "test" array as byte array.
deserialize returns your serialized bytearray as "test" array.
public static class TestSerializer{
public static byte[] Serialize(test[] t)
{
byte[] result;
BinaryFormatter bF = new BinaryFormatter();
using (MemoryStream mS = new MemoryStream())
{
bF.Serialize(mS, t);
mS.Position = 0;
result = new byte[mS.Length];
mS.Read(result, 0, result.Length);
}
return result;
}
public static test[] Deserialize(byte[] buffer)
{
test[] result;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream mS = new MemoryStream())
{
mS.Write(buffer, 0, buffer.Length);
mS.Position = 0;
result = (test[])bf.Deserialize(mS);
}
return result;
}
}
}
Call it like this.
test[] t = new test[200];
byte[] tBin = TestSerializer.Serialize(t);