从结构创建二进制块
我希望标题描述了问题,如果有人有更好的想法,我会更改它。
我将信息存储在这样的结构中:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您所需要的一切都在这个常见问题解答中进行了描述:
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
基本上,您将分配一个无符号
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 (usesizeof()
). Then you would copy the contents of each variable on the correct offset usingmemcpy
.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.
我现在使用 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.