public struct authseed
{
public UInt32 Index;
public UInt32 reslet;
public UInt16 nLen;
public Int64 p;
public Int64 q;
public Int64 r;
}
byte[] RecvBuf = new byte[0x1000];
authseed seed = RecvBuf ??? 怎么转换
{
public UInt32 Index;
public UInt32 reslet;
public UInt16 nLen;
public Int64 p;
public Int64 q;
public Int64 r;
}
byte[] RecvBuf = new byte[0x1000];
authseed seed = RecvBuf ??? 怎么转换
public struct authseed
{
public UInt32 Index;
public UInt32 reslet;
public UInt16 nLen;
public Int64 p;
public Int64 q;
public Int64 r;
} void Test()
{
byte[] RecvBuf = new byte[] {
1,0,0,0,
2,0,0,0,
3,0,
7,0,0,0,0,0,0,0,
8,0,0,0,0,0,0,0,
9,0,0,0,0,0,0,0,
}; GCHandle gch = GCHandle.Alloc(RecvBuf, GCHandleType.Pinned);
authseed seed = (authseed) Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof( authseed ) ); //<---
gch.Free();
}
Method 2 (easier to understand): public struct authseed
{
public UInt32 Index;
public UInt32 reslet;
public UInt16 nLen;
public Int64 p;
public Int64 q;
public Int64 r; public void GetValueFrom(byte[] buffer)
{
this.Index = (uint)BitConverter.ToInt32(buffer, 0);
this.reslet = (uint)BitConverter.ToInt32(buffer, 4);
this.nLen = (ushort)BitConverter.ToInt16(buffer, 8); int offset = 10; // offset = 12 if pack is 4 (aligned in 4 byte)! this.p = BitConverter.ToInt32(buffer, offset + 0);
this.q = BitConverter.ToInt32(buffer, offset + 8);
this.r = BitConverter.ToInt32(buffer, offset + 16);
}
}
void Test()
{
authseed seed = new authseed();
seed.GetValueFrom(RecvBuf); //<---
}