Oscar Yuandinata
Oscar Yuandinata

Reputation: 1035

convert a complex structure to byte array in c#

This is my structs' definition

    public struct SPMSifHdr
    {
        public UInt32 ui32Synch1;
        public UInt32 ui32Synch2;
        public ushort ui16Version;
        public UInt32 ui32Cmd;
        public UInt32 ui32BodySize;
    };

    struct SPMSifReturnKcdLclMsg
    {
        public SPMSifHdr hdr1;
        public char ff;
        public char[] Dta;
        public bool Debug;
        public char[] szOpId;
        public char[] szOpFirst;
        public char[] szOpLast;
    }

How to convert struct SPMSifReturnKcdLclMsg to array of bytes to send via tcp/ip?
Forgive my language, I can't use english very well :p

Upvotes: 1

Views: 1492

Answers (1)

alex.b
alex.b

Reputation: 4567

Socket socket = OpenSocket();
using (var stream = new NetworkStream(socket))
{
    var formatter = new BinaryFormatter();
    formatter.Serialize(stream, obj); 
}

EDIT:
Forgot to mention that your structs should be marked as Serializable

Upvotes: 3

Related Questions