There appeared a task to encode a binary data to base64 and then decode it back. There’re many solutions, but this one I liked very much.
#include "boost/archive/iterators/base64_from_binary.hpp"
#include "boost/archive/iterators/binary_from_base64.hpp"
#include "boost/archive/iterators/transform_width.hpp"
#include <string>
#include <iostream>
using namespace std;
using namespace boost::archive::iterators;
typedef
base64_from_binary<
transform_width<string::const_iterator, 6, 8>
> base64_t;
typedef
transform_width<
binary_from_base64<string::const_iterator>, 8, 6
> binary_t;
int main()
{
string str("Hello, world!");
cout << str << endl;
string enc(base64_t(str.begin()), base64_t(str.end()));
cout << enc << endl;
string dec(binary_t(enc.begin()), binary_t(enc.end()));
cout << dec << endl;
return 0;
}It’s simple enough, isn’t it? But the world is not ideal, and because of a bug decoding doesn’t work, as expected. Fortunately, the bug can be worked around with a little hack.