I was using the boost::multiprecision::uint128_t
type in order to perform bitwise operations on a 128 bit value. However I am having trouble writing the 128 bit value out to a binary file. Specifically with the need to pad out the value with zeros.
As an example if the uint128_t
value was 0x123456
then looking at the file in the hex editor I would want the sequence:
56 34 12 00 00 00 00 00 00 00 00 00 00 00 00 00
#include <boost/multiprecision/cpp_int.hpp>
#include <fstream>
boost::multiprecision::uint128_t temp = 0x123456;
std::ofstream ofile("test.bin", std::ios::binary);
ofile.write((char*)&temp, 16);
ofile.close();
Instead the binary file ends up with a value:
56 34 12 00 CC CC CC CC CC CC CC CC CC CC CC
I can see the the boost backend for the uint128_t
template appears to store the 128 bits as four 32 bit values. And has a "limb" value which indicates how many 32 bit values are in use. When the 32 bit values are not in use they are filled with 0xCCCCCCCC
. So the ofstream.write
is walking through the array of characters and writing out the 0xC
's.
Is there something I am missing in the boost library to help write this out correctly, or will I need to convert the uint128_t
value into some another data type?