Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..
i want to convert structure array to byte array..is it possible???
Posted
Comments
Member 10994712 13-Aug-14 1:17am    
is anybody know how to convert structure array to byte array and vice versa.?
Per Söderlund 13-Aug-14 3:31am    
Do you have an example?
Any code at all I can look at?
Member 10994712 13-Aug-14 4:15am    

struct test
{
public byte itemcode;
public string itemname;
//test* p;
}
test[] array = new test[200];
i want to convert this array to byte array is it posible????

Yes, you are looking for serialization.
reference this namespace.
C#
using System.Runtime.Serialization.Formatters.Binary;


Make your test struct serializable, like this.
C#
[Serializable]
    public struct test
    {
        public byte itemcode;
        public string itemname;
        //test* p;
    }

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.
C#
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.
C#
test[] t = new test[200];
byte[] tBin = TestSerializer.Serialize(t);
 
Share this answer
 
v3
Comments
Member 10994712 13-Aug-14 5:44am    
where i write this code??
Per Söderlund 13-Aug-14 6:55am    
I have updated my answer.
Member 10994712 14-Aug-14 1:06am    
when i write this code i getting an error Error 2 Inconsistent accessibility: parameter type 'byteconv.Form1.test[]' is less accessible than method 'byteconv.Form1.Serialize(byteconv.Form1.test[])' c:\users\adl65654\documents\visual studio 2012\Projects\byteconv\byteconv\Form1.cs 17 23 byteconv
Per Söderlund 15-Aug-14 9:58am    
Updated my solution again, sorry about that. I´m writing code in my browser without testing. Should be fine now.
Member 10994712 17-Aug-14 23:56pm    
i updated my program,but i got an error that --Error 1 An object reference is required for the non-static field, method, or property 'byteconv.Form1.TestSerializer.Serialize(byteconv.Form1.test[])' C:\Users\ADL65654\Documents\Visual Studio 2012\Projects\byteconv\byteconv\Form1.cs 205 27 byteconv
 
Share this answer
 
Comments
Member 10994712 12-Aug-14 7:44am    
i searched in google..but i dnt get any proper solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900