Arrays in C # dispensing structure (rpm)

https://blog.csdn.net/liuxf196921/article/details/21555541

In writing C # TCP communication program, send data, can only send a byte array to deal with them much trouble did not say, and if the program is written communications VC6.0 other words, many of the structures are transferred, in VC6. 0 can be easily put a char [] array into a structure, while in C #, but not directly to the byte array into a structure, to transmit structure, can be realized by the following method in C #:
(1) the definition of structure:

 

// namespace
using System.Runtime.InteropServices;

 

// Note that this attribute can not be less
[the StructLayoutAttribute (LayoutKind.Sequential, the CharSet = CharSet.Ansi, Pack =. 1)]
struct TestStruct
... {
public int C;
// max length of string, SizeConst a string of
[the MarshalAs ( UnmanagedType.ByValTStr, SizeConst = 256)]
public String STR;
// int array, the array represents the number of SizeConst, conversion to
// byte array before the array must be initialized before use, to initialize
the array length and must // SizeConst consistent new new int = Example Test [. 6];
[the MarshalAs (UnmanagedType.ByValArray, SizeConst =. 6)]
public int [] Test;
}
(2) transfer a byte array structure:

 

/ ** //// <Summary>
/// byte array transfer structure
/// </ Summary>
/// <param name = "structObj"> structure to be converted </ param>
/// <Returns > converted byte array </ Returns>
public static byte [] StructToBytes (Object structObj)
... {
// get the size of the structure
int size = Marshal.SizeOf (structObj);
// Create a byte array
byte [] bytes new new byte = [size];
// allocate memory space size structure
IntPtr structPtr = Marshal.AllocHGlobal (size);
// copying the structure of the allocated memory space
Marshal.StructureToPtr (structObj, structPtr, to false);
/ / from the memory array byte copying
Marshal.Copy (structPtr, bytes, 0, size);
// release memory space
Marshal.FreeHGlobal (structPtr);
// return byte array
return bytes;
}
(. 3) byte array transfer structure :

 

/ ** //// <Summary>
/// byte array to a structure
/// </ Summary>
/// <param name = "bytes"> byte array </ param>
/// <param name = " type "> structure type </ param>
/// <Returns> structure after conversion </ Returns>
public static Object BytesToStuct (byte [] bytes, the type type)
... {
// get the size structure
int = Marshal.SizeOf size (type);
// byte array length is less than the size of the structure of
IF (size> bytes.Length)
... {
// return the empty
return null;
}
// allocate memory space size structure
IntPtr structPtr Marshal.AllocHGlobal = (size);
// byte array to the allocated memory space copying
Marshal.Copy (bytes, 0, structPtr, size);
// memory space is converted to the target structure
object obj = Marshal.PtrToStructure ( structPtr, of the type);
// release the memory space
Marshal.FreeHGlobal (structPtr);
// return structure
return obj;
}

Guess you like

Origin www.cnblogs.com/xihong2014/p/12589637.html