字节数组转换成结构
據說是可以將字節數組轉換成結構。我還沒有測試。等要用的時候再來看看吧。先記到這兒。 using?System.Runtime.InteropServices;
static?object?BytesToStruct(byte[]?bytes,?Type?strcutType)
{
int?size?=?Marshal.SizeOf(strcutType);
IntPtr?buffer?=?Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes,?0,?buffer,?size);
return?Marshal.PtrToStructure(buffer,?strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
static?object?BytesToStruct(byte[]?bytes,?Type?strcutType)
{
int?size?=?Marshal.SizeOf(strcutType);
IntPtr?buffer?=?Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes,?0,?buffer,?size);
return?Marshal.PtrToStructure(buffer,?strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
轉載于:https://www.cnblogs.com/fuyun/archive/2005/02/07/103084.html
總結
- 上一篇: 我开始相信算命咯!
- 下一篇: Item 29. Virtual Con