about struct and byte array conversions in C #

Source: Internet
Author: User

Data transfer between sockets has recently been implemented using structs and byte array conversions. Now start tidying up. For Marshal, you can view MSDN, about byte arrays and struct-body transcoding as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Runtime.InteropServices;namespacefilesendclient{[StructLayoutAttribute (layoutkind.sequential, CharSet= CharSet.Ansi, Pack =1)]    structStructdemo { Public byteA;  Public byteC; [MarshalAs (UnmanagedType.ByValArray, SizeConst=3)]         Public byte[] b;  Public byteD;  Public inte; }    unsafe classProgram {Static voidMain (string[] args)            {Structdemo sd; SD.A=0; Sd.d=0; SD.C=0; SD.B=New byte[3] {0,0,1 }; SD.E=5; intSize =0; //use non-secure code here to get the value to Structdemo            unsafe{size=marshal.sizeof (SD); }                         byte[] B =structtobytes (sd,size); Bytetostruct (b,typeof(Structdemo)); }          //convert byte to struct type         Public Static byte[] Structtobytes (ObjectStructobj,intsize)            {Structdemo sd; intnum =2; byte[] bytes =New byte[size]; INTPTR structptr=Marshal.allochglobal (size); //copy the structure to the allocated memory spacemarshal.structuretoptr (Structobj, Structptr,false); //Copy from memory space to byte arraymarshal.copy (structptr, Bytes,0, size); //free up memory spaceMarshal.freehglobal (STRUCTPTR); returnbytes; }         //convert byte to struct type         Public Static ObjectBytetostruct (byte[] bytes, type type) {            intSize =marshal.sizeof (type); if(Size >bytes. Length) {return NULL; }            //allocating structure body storage spaceIntPtr structptr=Marshal.allochglobal (size); //copies a byte array to the allocated memory spacemarshal.copy (Bytes,0, structptr, size); //Convert the memory space to the target structure            Objectobj =marshal.ptrtostructure (structptr, type); //free up memory spaceMarshal.freehglobal (STRUCTPTR); returnobj; }    }}

about struct and byte array conversions in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.