Skip to content

Instantly share code, notes, and snippets.

@lingxd
Created June 8, 2022 09:18
Show Gist options
  • Save lingxd/8e8982b15b57cbd4092d3c6ba223aa22 to your computer and use it in GitHub Desktop.
Save lingxd/8e8982b15b57cbd4092d3c6ba223aa22 to your computer and use it in GitHub Desktop.
C++ to base64
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <utility>
#include <memory>
#include <string>
#include <vector>
std::string to_base64(const std::vector<uint8_t>& buffer) {
using namespace boost::archive::iterators;
using It = base64_from_binary<transform_width<std::vector<unsigned char>::const_iterator, 6, 8>>;
auto base64 = std::string(It(buffer.begin()), It(buffer.end()));
return base64.append((3 - buffer.size() % 3) % 3, '=');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment