2

How do I build a binary string in c++?

I want to have it in this format:

  std::string str = signed long (4bytes) "fill out zeros" 0x000 (8bytes) signed long (4bytes)

Note I DO NOT want the viewable binary representation but the actual binary data in the string

4

3 に答える 3

3

特定のアプリケーションの場合:

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);
于 2012-08-05T22:44:26.593 に答える
2
char buffer[1024];
// fill up buffer with whatever
char *end = afterLastChar;
std:string s(buffer, afterLastChar);
于 2012-08-05T22:03:59.453 に答える
0

Use std::vector< uint8_t >, this is not a string

于 2012-08-05T22:18:48.723 に答える