映射C数据结构和类型定义到.NET [英] Mapping C data structures and typedefs to .Net

查看:205
本文介绍了映射C数据结构和类型定义到.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个接口与网络协议通信,但IEEE文档描述了协议,在与信息分离翻过一个字节中位水平。

I'm trying to write an interface for communicating with a network protocol, but the IEEE document describes the protocol at the bit level with information split accross a single byte.

什么是着手处理一个C的typedef,如

What would be the best way to go about handling a C typedef such as

typedef struct {
   Nibble transportSpecific;
   Enumeration4 messageType;
   UInteger4 versionPTP;
   UInteger16 messageLength;
   UInteger8 domainNumber;
   Octet flagField[2];
   Integer64 correctionfield;
   PortIdentity sourcePortIdentity;
   UInteger16 sequenceId;
   UInteger8 controlField;
   Integer8 logMessageInterval;
} MsgHeader;

当移植一个兼容层到.NET?

when porting a compatibility layer to .Net?

推荐答案

FieldOffsetAttribute可能是对您有所帮助,虽然没有办法再present值不是一个字节的小。

FieldOffsetAttribute may be of help to you, although there is no way to represent values smaller than a byte.

我会用一个字节来重新present两个值互操作的目的,然后通过属性的getter访问的值。

I would use a byte to represent the two values for interop purposes, and then access the value via property getters.

unsafe struct MsgHeader
{
    public Nibble transportSpecific;
    //Enumeration4 messageType;
    //UInteger4 versionPTP;
    // use byte as place holder for these two fields
    public byte union;
    public ushort messageLength;
    public byte domainNumber;
    public fixed byte flagField[2];
    public long correctionfield;
    public PortIdentity sourcePortIdentity;
    public ushort sequenceId;
    public byte controlField;
    public sbyte logMessageInterval;

    // access value of two fields via getters
    public byte messageType { get { return (byte)(union >> 4); } }
    public byte versionPTP { get { return (byte)(union & 0xF); } }
}

这篇关于映射C数据结构和类型定义到.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆