在 C# 中表示位数组的最佳方式?

发布于 2024-08-29 06:42:52 字数 682 浏览 18 评论 0 原文

我目前正在 C# 中构建 DHCPMessage 类。

RFC 可在此处获取:http://www.faqs.org/rfcs/rfc2131.html

public object DHCPMessage
{
    bool[8] op;
    bool[8] htype;
    bool[8] hlen;
    bool[8] hops;
    bool[32] xid;
    bool[16] secs;
    bool[16] flags;
    bool[32] ciaddr;
    bool[32] yiaddr;
    bool[32] siaddr;
    bool[32] giaddr;
    bool[128] chaddr;
    bool[512] sname;
    bool[1024] file;
    bool[] options;
}

如果我们想象每个字段都是一个固定长度的位数组,那么什么是:

  1. 最通用的
  2. 最佳实践

将其表示为类的

方法???或者..你会怎么写这个? :)

I am currently building a DHCPMessage class in c#.

RFC is available here : http://www.faqs.org/rfcs/rfc2131.html

Pseudo

public object DHCPMessage
{
    bool[8] op;
    bool[8] htype;
    bool[8] hlen;
    bool[8] hops;
    bool[32] xid;
    bool[16] secs;
    bool[16] flags;
    bool[32] ciaddr;
    bool[32] yiaddr;
    bool[32] siaddr;
    bool[32] giaddr;
    bool[128] chaddr;
    bool[512] sname;
    bool[1024] file;
    bool[] options;
}

If we imagine that each field is a fixed length bit array, what is :

  1. The most versitile
  2. Best practice

way of representing this as a class???

OR.. how would you write this? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-09-05 06:42:52

对于初学者,您可以尝试 BitArray 类。无需在这里重新发明轮子。

如果您担心它占用太多空间/内存,请不要担心。只需将其初始化为正确的大小即可:(

BitArray op = new BitArray(8);

上面将保存 8 位,应占用 1 个字节)

For starters, you might try the BitArray class. No need to reinvent the wheel here.

If you're worried about it taking up too much space/memory, don't be. Just initialize it to the right size:

BitArray op = new BitArray(8);

(The above will hold 8 bits and should take up 1 byte)

情深已缘浅 2024-09-05 06:42:52

你走错了路,它不是位向量。该消息以“八位位组”定义,即“字节”。可以与 Marshal.PtrToStructure 一起使用的等效 C# 声明是:

    [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
    struct DHCPMessage {
        public byte op;
        public byte htype;
        public byte hlen;
        public byte hops;
        public uint xid;
        public ushort secs;
        public ushort flags;
        public uint ciaddr;
        public uint yiaddr;
        public uint siaddr;
        public uint giaddr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
        public byte[] chaddr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
        public string sname;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string file;
    }

您需要单独处理可变长度选项字段。

You are on the wrong track with this, it isn't a bit vector. The message is defined in "octets", better known as "bytes". An equivalent C# declaration that you can use with Marshal.PtrToStructure is:

    [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
    struct DHCPMessage {
        public byte op;
        public byte htype;
        public byte hlen;
        public byte hops;
        public uint xid;
        public ushort secs;
        public ushort flags;
        public uint ciaddr;
        public uint yiaddr;
        public uint siaddr;
        public uint giaddr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
        public byte[] chaddr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
        public string sname;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string file;
    }

You'll need to handle the variable length options field separately.

唠甜嗑 2024-09-05 06:42:52

您确定要对其中一些使用位数组吗?例如,您可以使用 byte 表示 8 位,使用 int 表示 32 位,并使用字节数组表示映射到以 null 结尾的字符串(例如“sname”)的片段。然后您可以使用简单的按位运算符(&、|)来检查/操作这些位。

以下是我关于将 TCP 标头转换为结构的一些帖子,其中还涵盖了字节序等。

http://taylorza.blogspot.com/2010/04/archive-struct-from-binary-data.html
http://taylorza.blogspot.com/2010/ 04/archive-binary-data-from-struct.html

这些已经很旧了,我将它们从我的旧博客迁移出来,这样它们就不会丢失。

Are you sure you want to be using bit arrays for some of these? For example, you can use byte for 8 bits, int for 32 bits, and byte arrays for pieces that map to null terminated strings like 'sname' for example. Then you can use simple bitwise operators (&, |) to check/manipulate the bits.

Here are some posts I did on converting TCP header to a structure, which also covers endianness etc.

http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html
http://taylorza.blogspot.com/2010/04/archive-binary-data-from-structure.html

These are quite old, I migrated them from my old blog just so they do not get lost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文