构建一个二进制字符串c++

build a binary string c++

本文关键字:二进制 字符串 c++ 一个 构建      更新时间:2023-10-16

如何在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>,这不是字符串