Skip to content

Instantly share code, notes, and snippets.

@tani
Created February 16, 2015 12:08
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 tani/3b6b497cff60f01d1ce1 to your computer and use it in GitHub Desktop.
Save tani/3b6b497cff60f01d1ce1 to your computer and use it in GitHub Desktop.
base64 decoder
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
int main(int argc, char *argv[])
{
std::string table =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string in,out;std::cin>>in;
std::string::iterator iter=in.begin();
std::bitset<24> data24;
for(int i=0;i<in.size()/4;i++){
data24|=(*iter=='=')?0:table.find(*iter,0);data24<<=6;++iter;
data24|=(*iter=='=')?0:table.find(*iter,0);data24<<=6;++iter;
data24|=(*iter=='=')?0:table.find(*iter,0);data24<<=6;++iter;
data24|=(*iter=='=')?0:table.find(*iter,0);++iter;
out.push_back((data24>>8*2).to_ulong());data24<<=8;
out.push_back((data24>>8*2).to_ulong());data24<<=8;
out.push_back((data24>>8*2).to_ulong());
}
std::cout<<out.c_str()<<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment