C # conversion from struct variable to byte [] variable

Source: Internet
Author: User

If you can convert the struct variable instance to the byte [] type, you can easily compress the variable storage. Below is a preliminary implementation of mine.

Using system. runtime. interopservices; # structure to be converted by region [structlayout (layoutkind. sequential, pack = 1)] // the alignment of the strutest variable in the memory (required for successful pointer type conversion) public struct strutest {public int intid; Public int intname; public datetime dtbegintime ;}# endregion /// <summary> /// Summary of xamconverter. Conversion between a specific structure variable and byte [] /// Author: Xam // Date: 2007-4-23 // note: apply convertion functions to deal with convertion between struct value and byte [] value; // </Summary> public class xamconverter {# region constructor public xamconverter () {// todo: add the constructor logic here //} # endregion # region method 1 forced conversion through pointer type # region structure variable to byte [] conversion // <summary> /// structure Variable to byte [] // </Summary> /// <Param name = "S"> Structure Variable </param> /// <returns> byte [] (allocated space) </returns> Public unsafe byte [] xamstructtobytes (strutest s) {byte [] arr = new byte [sizeof (strutest)]; fixed (byte * Parr = ARR) {* (strutest *) Parr) = s;} return arr ;} # endregion # conversion from Region byte [] to structural variables /// <summary> /// byte [] to conversion from structural variables /// </Summary> // <param name = "arr"> byte [] </param> // <returns> Structure Variable </returns> Public unsafe strutest xambytestostruct (byte [] ARR) {If (ARR. length <sizeof (strutest) throw new argumentexception (); strutest s; fixed (byte * Parr = ARR) {S = * (strutest *) Parr );} return s ;} # endregion # convert the region structure variable array to byte [] /// <summary> // convert the structure variable array to byte [] /// </Summary> // /<Param name = "S"> structure variable array </param> // <Param name = "tsize"> size of the structure variable array </param> // <returns> byte [] </returns> Public unsafe byte [] xamstructtobytes (strutest [] S, int tsize) {int I; byte [] arr = new byte [sizeof (strutest) * tsize]; for (I = 0; I <tsize; I ++) {fixed (byte * Parr = ARR) {(strutest *) Parr) [I]) = s [I] ;}} return arr ;} # endregion # conversion from Region byte [] to structure variable array // <summary> // byte [] to structure variable array // /<Param name = "arr"> byte [] </param> // <Param name = "tsize"> size of the array of Structure Variables contained in byte [] </Param >/// <returns> </returns> Public unsafe strutest [] xambytestostruct (byte [] arr, int tsize) {If (ARR. length <sizeof (strutest) * tsize) throw new argumentexception (); int I; strutest [] S = new strutest [tsize]; for (I = 0; I <tsize; I ++) {fixed (byte * Parr = ARR) {s [I] = (strutest *) Parr) [I]) ;}} return s ;} # endregion # region Mode 2 # conversion from Region Structure Variables to byte [] // <summary> // conversion from Structure Variables to byte [] /// </ summary> /// <Param name = "S"> Structure Variable </param> /// <returns> byte [] (allocated space) </returns> Public unsafe byte [] xamstructtobytesb (strutest s) {int size = marshal. sizeof (s); system. intptr = marshal. allochglobal (size); marshal. structuretoptr (S, PTR, true); byte [] BA = new byte [size]; marshal. copy (PTR, Ba, 0, ba. length); Return BA ;} # endregion # conversion from Region byte [] to structural variables /// <summary> /// byte [] to conversion from structural variables /// </Summary> // <param name = "arr"> byte [] </param> // <returns> Structure Variable </returns> Public unsafe strutest xambytestostructb (byte [] ARR) {strutest strureturn = new strutest (); int size = marshal. sizeof (strureturn); system. intptr = marshal. allochglobal (size); marshal. copy (ARR, 0, PTR, arr. length); strureturn = (strutest) Marshal. ptrtostructure (PTR, strureturn. getType (); Return strureturn ;} # endregion # convert the region structure variable array to byte [] /// <summary> // convert the structure variable array to byte [] /// </Summary> // /<Param name = "S"> structure variable array </param> // <Param name = "tsize"> size of the structure variable array </param> // <returns> byte [] </returns> Public unsafe byte [] xamstructtobytesb (strutest [] S, int tsize) {int I; int size = marshal. sizeof (s [0]) * s. length; system. intptr [] ptrs = new intptr [S. length]; for (I = 0; I <tsize; I ++) {ptrs [I] = marshal. allochglobal (size); marshal. structuretoptr (s [I], ptrs [I], true);} byte [] BA = new byte [size]; for (I = 0; I <tsize; I ++) {marshal. copy (ptrs [I], Ba, I * marshal. sizeof (s [0]), Marshal. sizeof (s [0]);} return BA ;} # endregion # conversion from Region byte [] to structure variable array // <summary> // byte [] to structure variable array // /<Param name = "arr"> byte [] </param> // <Param name = "tsize"> size of the array of Structure Variables contained in byte [] </Param >/// <returns> </returns> Public unsafe strutest [] xambytestostructb (byte [] arr, int tsize) {If (ARR. length <sizeof (strutest) * tsize) throw new argumentexception (); strutest [] strureturn = new strutest [tsize]; int intunitsize = marshal. sizeof (strureturn [0]); int size = intunitsize * tsize; int I; int inttemp; system. intptr [] ptrs = new intptr [tsize]; for (I = 0; I <tsize; I ++) {inttemp = I * intunitsize; ptrs [I] = marshal. allochglobal (size); marshal. copy (ARR, inttemp, ptrs [I], intunitsize); strureturn [I] = (strutest) Marshal. ptrtostructure (ptrs [I], strureturn [I]. getType ();} return strureturn;} # endregion}

Enable the Unsafe code Option

Demo

# Region structure array test xamconverter XC = new xamconverter (); strutest [] ST = new strutest [3]; ST [0]. intid = 1; ST [0]. intname = 12; ST [0]. dtbegintime = datetime. now; ST [2]. intid = 2; ST [2]. intname = 232; ST [2]. dtbegintime = datetime. now; byte [] inputbytes = XC. xamstructtobytesb (St, 3); strutest [] st2 = XC. xambytestostructb (inputbytes, 3); MessageBox. show (st2 [2]. dtbegintime. tostring () + "=" + st2 [2]. intname. tostring (); # endregion

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.