从结构创建二进制块

发布于 2024-09-05 12:30:38 字数 1051 浏览 2 评论 0原文

我希望标题描述了问题,如果有人有更好的想法,我会更改它。

我将信息存储在这样的结构中:

struct AnyStruct
{
    AnyStruct() :
        testInt(20),
        testDouble(100.01),
        testBool1(true),
        testBool2(false),
        testBool3(true),
        testChar('x') {}

    int testInt;
    double testDouble;
    bool testBool1;
    bool testBool2;
    bool testBool3;
    char testChar;

    std::vector<char> getBinaryBlock()
    {
        //how to build that?
    }
}

该结构应通过网络在二进制字节缓冲区中发送,具有以下结构:

Bit 00- 31: testInt
Bit 32- 61: testDouble most significant portion
Bit 62- 93: testDouble least significant portion
Bit     94: testBool1
Bit     95: testBool2
Bit     96: testBool3
Bit 97-104: testChar

根据此定义,生成的 std::vector 的大小应为 13 个字节(char ==字节)

我现在的问题是如何从我拥有的不同数据类型中形成这样的数据包。我已经阅读了很多页面并找到了像 std::bitset 或 boost::dynamic_bitset 这样的数据类型,但似乎都没有解决我的问题。

我认为很容易看出,上面的代码只是一个示例,原始标准要复杂得多并且包含更多不同的数据类型。我认为解决上面的例子也应该解决我的复杂结构问题。

最后一点: 这个问题应该通过使用标准的、可移植的 C++ 语言特性(如 STL 或 Boost)来解决(

I hope the title is describing the problem, i'll change it if anyone has a better idea.

I'm storing information in a struct like this:

struct AnyStruct
{
    AnyStruct() :
        testInt(20),
        testDouble(100.01),
        testBool1(true),
        testBool2(false),
        testBool3(true),
        testChar('x') {}

    int testInt;
    double testDouble;
    bool testBool1;
    bool testBool2;
    bool testBool3;
    char testChar;

    std::vector<char> getBinaryBlock()
    {
        //how to build that?
    }
}

The struct should be sent via network in a binary byte-buffer with the following structure:

Bit 00- 31: testInt
Bit 32- 61: testDouble most significant portion
Bit 62- 93: testDouble least significant portion
Bit     94: testBool1
Bit     95: testBool2
Bit     96: testBool3
Bit 97-104: testChar

According to this definition the resulting std::vector should have a size of 13 bytes (char == byte)

My question now is how I can form such a packet out of the different datatypes I've got. I've already read through a lot of pages and found datatypes like std::bitset or boost::dynamic_bitset, but neither seems to solve my problem.

I think it is easy to see, that the above code is just an example, the original standard is far more complex and contains more different datatypes. Solving the above example should solve my problems with the complex structures too i think.

One last point:
The problem should be solved just by using standard, portable language-features of C++ like STL or Boost (

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

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

发布评论

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

评论(3

小梨窩很甜 2024-09-12 12:30:38

我认为您所需要的一切都在这个常见问题解答中进行了描述:
http://www.parashift.com/c++-faq-lite/serialization。 html

I think all you need is described in this FAQ:
http://www.parashift.com/c++-faq-lite/serialization.html

伤痕我心 2024-09-12 12:30:38

基本上,您将分配一个无符号 char (BYTE) 数组,其长度等于类中所有变量的总大小(使用 sizeof() )。然后,您可以使用 memcpy 将每个变量的内容复制到正确的偏移量上。

请注意,这只是一种基本方法,我绝对建议您查看 zerm 答案中的 c++ 常见问题解答链接。

Basically you would allocate an array of unsigned char (BYTEs) of the length of the total size of all variables in your class (use sizeof() ). Then you would copy the contents of each variable on the correct offset using memcpy.

Note that this is just a basic approach and I would definetly suggest to take a look at the c++ faq link in zerm's answer.

£冰雨忧蓝° 2024-09-12 12:30:38

我现在使用 std::bitset 来生成数据类型的位序列,然后将这些位集连接到一个大位集。
然后它保存我需要的数据。我只需要检查小/大端。

I've now used the std::bitset for generating bit-sequences for the datatypes and then concatenated these bitsets to one big bitset.
This then holds the data I need. I just have to check for little/big endian.

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