如何在 C++ 中构建二进制字符串?
我想以这种格式拥有它:
std::string str = signed long (4bytes) "fill out zeros" 0x000 (8bytes) signed long (4bytes)
注意我不想要可见的二进制表示,而是字符串中的实际二进制数据
对于您的特定应用:
std::string data(12, 0); // construct string of 12 null-bytes
int32_t x, y; // populate
char const * const p = reinterpret_cast<char const *>(&x);
char const * const q = reinterpret_cast<char const *>(&y);
std::copy(p, p + 4, data.begin() );
std::copy(q, q + 4, data.begin() + 8);
char buffer[1024];
// fill up buffer with whatever
char *end = afterLastChar;
std:string s(buffer, afterLastChar);
使用 std::vector<uint8_t>,这不是字符串