Reputation: 155
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
Upvotes: 2
Views: 3304
Reputation: 476940
For your specific application:
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);
Upvotes: 3
Reputation: 3522
char buffer[1024];
// fill up buffer with whatever
char *end = afterLastChar;
std:string s(buffer, afterLastChar);
Upvotes: 2