积极答复者
c++结构体翻译为c#结构体

问题
-
c++原结构体如下
typedef struct { /* CAN_MSG */
union {
UINT8 id[4];
UINT32 identifier;
};
union {
struct {
UINT8 length :4; /* data length */
UINT8 resbit :2;
UINT8 remote :1; /* remote transmission request */
UINT8 format :1; /* frame format */
};
UINT8 info;
};
UINT8 reserve[3];
UINT8 data[8]; /* data field */
} CAN_MSG;
答案
-
lxg23 你好,
欢迎来到MSDN论坛!
以下是改写后的C#代码,注释部分是在C#代码中需要注意的地方:
using System.Runtime.InteropServices;
public class CAN_MSG // CAN_MSG
{
//Unions are not supported in C#, but the following union can be simulated with the StructLayout and FieldOffset attributes.
//ORIGINAL LINE: union
//Structs must be named in C#, so the following struct has been named AnonymousStruct:
[StructLayout(LayoutKind.Explicit)]
public struct AnonymousStruct
{
[FieldOffset(0)]
public byte[] id = new byte[4];
[FieldOffset(0)]
public uint identifier;
}
//Unions are not supported in C#.
//ORIGINAL LINE: union
//Structs must be named in C#, so the following struct has been named AnonymousStruct2:
public struct AnonymousStruct2
{
//Classes must be named in C#, so the following class has been named AnonymousClass:
public class AnonymousClass
{
//C# does not allow bit fields:
public byte length :4; // data length
public byte resbit :2;
public byte remote :1; // remote transmission request
public byte format :1; // frame format
}
public byte info;
}
public byte[] reserve = new byte[3];
public byte[] data = new byte[8]; // data field
}
希望这些内容对你有所帮助。
谢谢你对MSDN论坛的支持!
Mio
Mio Miao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 lxg23 2010年12月24日 6:33
全部回复
-
c++原结构体如下
typedef struct { /* CAN_MSG */
union {
UINT8 id[4];
UINT32 identifier;
};
union {
struct {
UINT8 length :4; /* data length */
UINT8 resbit :2;
UINT8 remote :1; /* remote transmission request */
UINT8 format :1; /* frame format */
};
UINT8 info;
};
UINT8 reserve[3];
UINT8 data[8]; /* data field */
} CAN_MSG; -
lxg23 你好,
欢迎来到MSDN论坛!
以下是改写后的C#代码,注释部分是在C#代码中需要注意的地方:
using System.Runtime.InteropServices;
public class CAN_MSG // CAN_MSG
{
//Unions are not supported in C#, but the following union can be simulated with the StructLayout and FieldOffset attributes.
//ORIGINAL LINE: union
//Structs must be named in C#, so the following struct has been named AnonymousStruct:
[StructLayout(LayoutKind.Explicit)]
public struct AnonymousStruct
{
[FieldOffset(0)]
public byte[] id = new byte[4];
[FieldOffset(0)]
public uint identifier;
}
//Unions are not supported in C#.
//ORIGINAL LINE: union
//Structs must be named in C#, so the following struct has been named AnonymousStruct2:
public struct AnonymousStruct2
{
//Classes must be named in C#, so the following class has been named AnonymousClass:
public class AnonymousClass
{
//C# does not allow bit fields:
public byte length :4; // data length
public byte resbit :2;
public byte remote :1; // remote transmission request
public byte format :1; // frame format
}
public byte info;
}
public byte[] reserve = new byte[3];
public byte[] data = new byte[8]; // data field
}
希望这些内容对你有所帮助。
谢谢你对MSDN论坛的支持!
Mio
Mio Miao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 lxg23 2010年12月24日 6:33