C # convert struct into byte streams,
1. Convert the basic type to byte array storage
1 private byte[] CreateNetDataByteStream(ushort system, ushort host, ushort type, byte[] tx_buf, ushort msg_len, ushort flag) 2 3 { 4 5 if (tx_buf == null) 6 7 { 8 9 return null;10 11 }12 13 try14 15 {16 17 byte[] data = new byte[msg_len + NetDataHeadLen];18 19 byte[] u16byte = new byte[2];20 21 u16byte = BitConverter.GetBytes(type);22 23 Array.Copy(u16byte, 0, data, 0, 2);24 25 u16byte = BitConverter.GetBytes(flag);26 27 Array.Copy(u16byte, 0, data, 4, 2);28 29 u16byte = BitConverter.GetBytes(msg_len);30 31 Array.Copy(u16byte, 0, data, 2, 2);32 33 // u16byte = BitConverter.GetBytes(CommonConstant.MySystemID);34 35 Array.Copy(u16byte, 0, data, 6, 2);36 37 // u16byte = BitConverter.GetBytes((ushort)CommonConstant.MySeatName);38 39 Array.Copy(u16byte, 0, data, 8, 2);40 41 u16byte = BitConverter.GetBytes(system);42 43 Array.Copy(u16byte, 0, data, 15, 2);44 45 u16byte = BitConverter.GetBytes(host);46 47 Array.Copy(u16byte, 0, data, 17, 2);48 49 tx_buf.CopyTo(data, NetDataHeadLen);50 51 return data;52 53 }54 55 catch56 57 {58 return null;59 60 }61 62 }
2. in C #, struct and byte streams are converted to each other.
Method 1 // serialize a structure to a byte array private IFormatter formatter = new BinaryFormatter (); private ValueType deserializeByteArrayToInfoObj (byte [] bytes) {ValueType vt; if (bytes = null | bytes. length = 0) {return null;} try {MemoryStream stream = new MemoryStream (bytes); stream. position = 0; stream. seek (0, SeekOrigin. begin); vt = (ValueType) formatter. deserialize (stream); stream. close (); return vt;} catch (Exception ex) {return null ;}// serialize a structure to a byte array private byte [] serializeInfoObjToByteArray (ValueType infoStruct) {if (infoStruct = null) {return null;} try {MemoryStream stream = new MemoryStream (); formatter. serialize (stream, infoStruct); byte [] bytes = new byte [(int) stream. length]; stream. position = 0; int count = stream. read (bytes, 0, (int) stream. length); stream. close (); return bytes;} catch (Exception ex) {return null ;}}
Method 2 // <summary> /// convert a byte array to a struct // </summary> /// <param name = "bytes"> </param>/ // <param name = "type"> </param> // <returns> </returns> public object ByteaToStruct (byte [] bytes, type type) {// get the struct size int size = Marshal. sizeOf (type); Math. log (size, 1); if (size> bytes. length) return null; // allocate the memory size of the structure IntPtr structPtr = Marshal. allocHGlobal (size); // copy the BYTE array to the allocated memory space Marshal. copy (bytes, 0, structPtr, size); // converts the memory space to the Target Structure object obj = Marshal. ptrToStructure (structPtr, type); // release the content space Marshal. freeHGlobal (structPtr); return obj ;} /// <summary> /// convert the structure to a byte array /// </summary> /// <param name = "obj"> </param> /// <returns> </returns> public byte [] StructTOBytes (object obj) {int size = Marshal. sizeOf (obj); // create byte array byte [] bytes = new byte [size]; IntPtr structPtr = Marshal. allocHGlobal (size); // copy the struct to the allocated memory. structureToPtr (obj, structPtr, false); // copy from memory space to byte array Marshal. copy (structPtr, bytes, 0, size); // release memory space Marshal. freeHGlobal (structPtr); return bytes ;}
3. C # structure byte alignment
1 [structLayout(Layoutkind.sequential,charset=charset.ansi)]2 Struct Mystruct3 {4 [MarshalAs(UnmanagedType.ByValArray,sizeConst=8)]5 Public byte[] serial;6 Public byte Type;7 Public uint Sum;8 }
In the second method of converting the above struct and byte stream, get the struct length int size = Marshal. SizeOf (Mystruct);, instead of 13, but 16. The starting address of a specific data structure in the memory usually has a certain alignment requirement. For example, the starting address of a 32-bit machine's int must be an integer multiple of 4.
You need to add [structLayout (Layoutkind. sequential, charset = charset. ansi, pack = 1)]