将昨天的php代码改造成C++
/*base_64.h文件*/ #ifndef BASE_64_H #define BASE_64_H /** * Base64 编码/解码 * @author liruixing */ class Base64{ private: std::string _base64hash; int _encode_data; int _decode_data; int _width; bool _debug; std::string _encode_result,_decode_result; void _encode_func(int bin,int bytes = 3); void _decode_func(int bin,int bytes = 2); public: Base64() { _base64hash = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /*这是Base64编码使用的标准字典*/ _encode_data = 0xfc0000; _decode_data = 0xff0000; _width = 0xffffff; _debug = false; } /** * 这里必须是unsigned类型,否则编码中文的时候出错 */ std::string Encode(const unsigned char * str,int bytes); std::string Decode(const char *str,int bytes); void Debug(bool open = true); }; #endif
上面定义了一个头文件,定义base64的类
/*base_64.cpp文件*/ #include <iostream> #include <string> #include <cstring> #include "base_64.h" void Base64::_encode_func(int bin,int bytes) { int num = 3,matches = 0,bits1=0,bits2=0,result=0; bits1 = (num - bytes) * 6; bits2 = bytes * 6; matches = _encode_data >> bits1; while( matches ) { result = bin & matches; result = result >> bits2; bytes--; bits1 = (num - bytes) * 6; bits2 = bytes * 6; matches = _encode_data >> bits1; if(_debug) { printf("result = %d , hex = %x\n",result,result); } _encode_result += _base64hash[result]; } } void Base64::_decode_func(int bin,int bytes) { int num = 2,matches = 0,bits1=0,bits2=0,result=0; bits1 = (num - bytes) * 8; bits2 = bytes * 8; matches = _decode_data >> bits1; while( matches ) { result = bin & matches; result = result >> bits2; bytes--; bits1 = (num - bytes) * 8; bits2 = bytes * 8; matches = _decode_data >> bits1; if(_debug) { printf("result = %d , hex = %x\n",result,result); } _decode_result += (char)result; } } std::string Base64::Encode(const unsigned char * str,int bytes) { int num = 0,bin = 0,i; if(bytes >= 3) { for(i=0;i<bytes;i++) { bin = bin << 8; if(_debug) { printf("bin = %d , hex = %x\n",bin,str[i]); } bin = bin | (int)str[i]; if((i+1)%3 == 0) { _encode_func(bin,3); bin = 0; } } } if(bytes%3 == 1) { bin = (int)str[bytes-1]; bin = bin << 4; _encode_func(bin,1); _encode_result += "=="; } else if(bytes%3 == 2) { bin = (int)str[bytes-2]; bin = bin << 8; bin = bin | (int)str[bytes-1]; bin = bin << 2; _encode_func(bin,2); _encode_result += "="; } return _encode_result; } std::string Base64::Decode(const char *str,int bytes) { //解码表 const char DecodeTable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // ‘+‘ 0, 0, 0, 63, // ‘/‘ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // ‘0‘-‘9‘ 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // ‘A‘-‘Z‘ 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // ‘a‘-‘z‘ }; int bin = 0,i,pos=0; char *_decode_string = new char[bytes]; strcpy(_decode_string,str); while(_decode_string[bytes-1] == ‘=‘) { _decode_string[bytes-1] = ‘\0‘; bytes--; } if(bytes >= 4) { for(i=0;i<bytes;i++) { bin = bin << 6; pos = DecodeTable[(int)_decode_string[i]]; if(_debug) { printf("bin = %d , binary = %x-----ch = %c\n",bin,pos,_decode_string[i]); } bin = bin | pos; if((i+1)%4 == 0) { _decode_func(bin); bin = 0; } } } if(bytes%4 == 3) { bin = 0; if(_debug) { printf("bin = %x , ch = %c\n",bin, _decode_string[bytes-3]); } pos = DecodeTable[(int)_decode_string[bytes-3]]; bin = bin | pos; bin = bin << 6; if(_debug) { printf("bin = %x , ch = %c\n",bin, _decode_string[bytes-2]); } pos = DecodeTable[(int)_decode_string[bytes-2]]; bin = bin | pos; bin = bin << 6; if(_debug) { printf("bin = %x , ch = %c\n",bin, _decode_string[bytes-1]); } pos = DecodeTable[(int)_decode_string[bytes-1]]; bin = bin | pos; if(_debug) { printf("bin = %x\n",bin); } bin = bin >> 2; _decode_func(bin,1); } else if(bytes%4 == 2) { bin = 0; if( _debug ) { printf("bin = %x , ch = %c\n",bin, _decode_string[bytes-2]); } pos = DecodeTable[(int)_decode_string[bytes-2]]; bin = bin | pos; bin = bin << 6; if( _debug ) { printf("bin = %x , ch = %c\n",bin, _decode_string[bytes-1]); } pos = DecodeTable[(int)_decode_string[bytes-1]]; bin = bin | pos; bin = bin >> 4; _decode_func(bin,0); } delete [] _decode_string; return _decode_result; } void Base64::Debug(bool open) { _debug = open; }
base64类中方法的定义实际上是在base_64.cpp中进行的。
上面的两个文件用来生成一个静态链接库:libbase_64.a
g++ -c base_64.cpp ar rs libbase_64.a base_64.o
下面来进行实际的测试:
/*main.cppe文件*/ #include <iostream> #include "base_64.h" using namespace std; int main() { unsigned char str[] = "这是一条测试数据:http://img.v.cmcm.com/7dd6e6d7-0c2d-5a58-a072-71f828b94cbc_crop_216x150.jpg"; string normal,encoded; int i,len = sizeof(str)-1; Base64 *base = new Base64(); encoded = base->Encode(str,len); cout << "base64 encode : " << encoded << endl; len = encoded.length(); const char * str2 = encoded.c_str(); normal = base->Decode(str2,len); cout << "base64 decode : " << normal <<endl; return 0; }
编译代码并运行
g++ main.cpp -L. -lbase_64 -Ibase_64 -o main ./main
输出效果:
在改造昨天的php代码过程中,还参考了cnblogs上洞庭散人的代码实现的。
参考:
http://www.cnblogs.com/phinecos/archive/2008/10/10/1308272.html
时间: 2024-10-07 19:14:24