std::ios::binary promises to not do any line-end conversions on the stream (and some other small behavioral differences with text streams).
You could look at
- Boost Serialization http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html
- Boost Spirit binary generators http://www.boost.org/doc/libs/1_53_0/libs/spirit/doc/html/spirit/karma/reference/binary/
- Using ofstream::write(…) to manually write the bytes
Here’s an example using Boost Spirit Karma (assuming Big-Endian byte ordering):
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
int main()
{
int i = 10;
char c="c";
float f = 30.40f;
std::ostringstream oss(std::ios::binary);
oss << karma::format(
karma::big_dword << karma::big_word << karma::big_bin_float,
i, c, f);
for (auto ch : oss.str())
std::cout << std::hex << "0x" << (int) (unsigned char) ch << " ";
std::cout << "\n";
}
This prints
0x0 0x0 0x0 0xa 0x0 0x63 0x41 0xf3 0x33 0x33