Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active January 8, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MORTAL2000/05f5b100166203ed3cc7 to your computer and use it in GitHub Desktop.
Save MORTAL2000/05f5b100166203ed3cc7 to your computer and use it in GitHub Desktop.
#include <SFML/Graphics.hpp>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <array>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64_encode(const std::string& data)
{
std::string ret;
std::array<unsigned char, 3> char_array;
for (auto begin = data.begin(), end = data.end(), it = begin; it != end; ++it)
{
auto index = it - begin;
// Read a group of three bytes (avoid buffer overrun by replacing with 0)
char_array[index % char_array.size()] = *it;
if (index % char_array.size() != char_array.size() - 1) continue;
// Transform into four base 64 characters
ret += base64_chars[(char_array[0] & 0xfc) >> 2];
ret += base64_chars[((char_array[0] & 0x03) << 4) + ((char_array[1] & 0xf0) >> 4)];
ret += base64_chars[((char_array[1] & 0x0f) << 2) + ((char_array[2] & 0xc0) >> 6)];
ret += base64_chars[((char_array[2] & 0x3f) << 0)];
}
return ret;
}
std::string stringFromFile(const std::string& file)
{
std::ifstream fin;
fin.open(file, std::ios::binary);
std::ostringstream out;
out << fin.rdbuf();
fin.close();
return out.str();
}
int main()
{
std::string encoded = base64_encode(stringFromFile("sfml-logo-small.png"));
std::ofstream file;
file.open("sfml-logo-small.pin");
file << encoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment