views:

243

answers:

1

Hello,

I'm trying to translate the following code from C++ to C#

`

struct tPacket
{
WORD size;
WORD opcode;
BYTE securityCount;
BYTE securityCRC;
}

...

static char data[8192] = {0};
tPacket * packet = (tPacket *)data;`

so far I've come up with:

C#

public struct tPacket
{
public ushort size;
public ushort opcode;
public byte securityCount;
public byte securityCRC;
}     

public static byte[] data = new byte[1024];
tPacket packet = new tPacket();
packet = (tPacket *)data;

However,I get an error "Cannot convert type 'byte[]' to 'MyNameSpace.tPacket*"

The whole code is used to put a received packet buffer(data) into a structure then directly access the structure members. Any help will be appreciated! Thanks in advance.

+2  A: 

Here's a link that might help:

http://varjabedian.net/archive/2008/07/29/casting-a-byte-array-into-structures-in-c.aspx

Andy White
Thanks,i will try it later,but it seems it will do the job!
John