base62エンコードの処理を作ってみた
今回のプログラムは珍しく、VC6ではコンパイルできません。*1g++あたりでコンパイルできるんじゃないかな?itoaをパクッてint to stringという事でitosという名前にしました。2進法から62進法まで対応する珍しい一品。私はこの手の処理に自信が無いのでバグがあるかも!?
http://ideone.com/H6ly2
//via http://hi.baidu.com/beyoniger/blog/item/1ef9dbcd3b24d8550fb345d0.html //http://github.com/sinefunc/base62/blob/master/base62.rb to C++ #include <iostream> #include <string> #include <algorithm> std::string itos(unsigned long long value,int base){ std::string out;// check that the base if valid if (base < 2 || base > 62) { return out; } unsigned long long absQModB; unsigned long long q = value; const char *ascii_table="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; while(q){ absQModB=q % base; //if (absQModB < 0) // absQModB=-absQModB; out += ascii_table[ absQModB ]; q /= base; } std::reverse( out.begin(), out.end() ); return out; } int main(){ return "lYGhA16ahyf"==itos(18446744073709551615ULL,62); }