访问结构成员的指针 [英] Accessing structure members with a pointer
本文介绍了访问结构成员的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从C ++下面的代码转换为C#
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;
不过,我得到一个错误
无法将类型'字节[]'到' MyNameSpace.tPacket *
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.
推荐答案
下面是一个链接,可以帮助:
Here's a link that might help:
http://varjabedian.net/archive/2008/07/29/casting-a-byte-array-into-structures-in-c.aspx
这篇关于访问结构成员的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文