Last active
August 29, 2015 14:15
-
-
Save tani/f1ac46fa7a62b0a0feed to your computer and use it in GitHub Desktop.
base64 encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
#include <vector> | |
#include <string> | |
#include <cmath> | |
int main(int argc, char *argv[]) | |
{ | |
std::string table= | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789+/"; | |
std::string in,out;std::cin>>in; | |
std::string::iterator iter; | |
std::bitset<24> data24; | |
int blank=ceil(in.size()/3.0)*3-in.size(); | |
in.resize(ceil(in.size()/3.0)*3); | |
for(int i=0;i<in.size()/3;i++){ | |
data24|=*iter++;data24<<=8; | |
data24|=*iter++;data24<<=8; | |
data24|=*iter++; | |
out+=table.at((data24>>6*3).to_ulong());data24<<=6; | |
out+=table.at((data24>>6*3).to_ulong());data24<<=6; | |
out+=table.at((data24>>6*3).to_ulong());data24<<=6; | |
out+=table.at((data24>>6*3).to_ulong()); | |
} | |
for(int i=1;i<=blank;i++) | |
out.at(out.size()-i)='='; | |
std::cout<<out<<std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment