如何将结构体所需的位打包到 char* 中?

发布于 2024-09-26 00:07:52 字数 210 浏览 1 评论 0原文

语言:C++

我正在研究位打包(从给定数据中提取所需的位并将它们打包在 char* 中)。我的代码目前支持: - 整数 - 人物 - 字符串

现在,如果我必须存储结构所需的位,我应该如何处理?我的意思是我应该期望什么作为通用代码结构的输入参数?

这个问题可能很模糊,我不期待直接的答案,即使是提示和指示也是值得赞赏的。

Language : C++

I am working on Bit Packing (Extracting the required bits from the given data and packing them in a char*) . My code currently supports :
- Integers
- Characters
- Strings

Now if I have to store the required bits of a structure, how should I go about it ? I mean what should I expect as input parameters for a generalized code w.r.t structures ?

The question may be vague and I am not expecting direct answers, even hints and pointers are appreciated.

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

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

发布评论

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

评论(3

猛虎独行 2024-10-03 00:07:52

查看 this 以获得非常紧凑的格式或使用标准编组格式,例如 json 、 xml、增强序列化...并拯救自己的白发。

Have a look at this for a very packed format or use an standard marshalling format such as json, xml, boost serialization,... and save yourself the grey hair.

断肠人 2024-10-03 00:07:52

正如 piotr 已经建议的那样:尝试使用现有的库来编组。

但是,由于您已经自己做了:
如果您支持的基元可表示为字节,那么您不应该进行位打包(该术语可能会产生一些混淆),否则请考虑使用 std::bitset
因为C++不支持反射,所以C++对字节包没有任何帮助以通用、安全和可移植的方式构建结构,因此准备为每个结构编写一个函数来打包每个原语和每个成员结构。
重载确实有助于以通用方式调用这些函数,因此可以通用地完成容器(向量...)的打包。但是,如果您想要这样做,那么优先选择自由函数而不是成员函数,以避免打包基元和打包结构之间存在差异。

例子:

void Pack(const MyStruct& str, Packer& pack)
{
  Pack(str.int1, pack);
  Pack(str.string1, pack);
  Pack(str.otherStruct, pack);
}  

As piotr already suggested: try using existing libraries to marshall.

However, since your already doing it yourself:
If your supported primitives are representable as bytes, then you shouldn't be bit packing (there might be some confusion about the term), otherwise consider using std::bitset.
Because C++ doesn't support reflection, there is no help in C++ to byte pack structures in a generic, safe and portable way, so be prepared to write a function per structure to pack each primitive and each member structure.
Overloading does help to call these functions in a generic way, so packing of containers (vector ...) can be done generically. However, if you want this, then prefer free functions over member functions, to avoid having a difference between packing primitives and packing structures.

Example:

void Pack(const MyStruct& str, Packer& pack)
{
  Pack(str.int1, pack);
  Pack(str.string1, pack);
  Pack(str.otherStruct, pack);
}  
飘过的浮云 2024-10-03 00:07:52

您可以使用reinterpret_cast()来访问该结构,就像它是一个char*一样:

#include <iostream>
using namespace std;

struct myStruct
{
    char a; int b;
};

int main(int argc, char* argv[])
{
    myStruct instance = { 10, 100 };

    //Treat the myStruct instance as though it were a char*
    char* pchar = reinterpret_cast<char*>(&instance);

    //Output the values of the bytes to the console
    for(int i = 0; i < sizeof(instance); i++)
        cout << (int)pchar[i] << endl;

    getchar();
    return 0;
}

但是请注意,由于对齐的原因,并不是所有的char 将包含有意义的数据。至少在 Visual Studio 中可以使用 来修复此问题pack pragma..但结果仍然高度依赖于体系结构/编译器。

因此,如果您想以一种完全可移植/可维护的方式打包字节(即您将在现实世界中使用它),我强烈建议使用 piotr 提到的序列化方法之一。

You can use reinterpret_cast<char*>() to access the struct as though it were a char*:

#include <iostream>
using namespace std;

struct myStruct
{
    char a; int b;
};

int main(int argc, char* argv[])
{
    myStruct instance = { 10, 100 };

    //Treat the myStruct instance as though it were a char*
    char* pchar = reinterpret_cast<char*>(&instance);

    //Output the values of the bytes to the console
    for(int i = 0; i < sizeof(instance); i++)
        cout << (int)pchar[i] << endl;

    getchar();
    return 0;
}

Note, however, that because of alignment, not all of the char's will contain meaningful data. This can be fixed, in visual studio at least, using the pack pragma.. but the results will still be highly architecture/compiler-dependent.

Thus, if you want to pack the bytes in a way that is at all portable/maintainable (ie. you're going to use this in the real-world), I would highly suggest using one of the serialization methods mentioned by piotr.

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