Base64编解码(C++版)

复制代码
#include < string>
using  namespace std;

class ZBase64
{
public:
     /* 编码
    DataByte
        [in]输入的数据长度,以字节为单位
    
*/
     string Encode( const unsigned  char* Data, int DataByte);
     /* 解码
    DataByte
        [in]输入的数据长度,以字节为单位
    OutByte
        [out]输出的数据长度,以字节为单位,请不要通过返回值计算
        输出数据的长度
    
*/
     string Decode( const  char* Data, int DataByte, int& OutByte);
};
复制代码

复制代码
#include  " stdAfx.h "
#include  " ZBase64.h "

string ZBase64::Encode( const unsigned  char* Data, int DataByte)
{
     // 编码表
     const  char EncodeTable[]= " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ ";
     // 返回值
     string strEncode;
    unsigned  char Tmp[ 4]={ 0};
     int LineLength= 0;
     for( int i= 0;i<( int)(DataByte /  3);i++)
    {
        Tmp[ 1] = *Data++;
        Tmp[ 2] = *Data++;
        Tmp[ 3] = *Data++;
        strEncode+= EncodeTable[Tmp[ 1] >>  2];
        strEncode+= EncodeTable[((Tmp[ 1] <<  4) | (Tmp[ 2] >>  4)) &  0x3F];
        strEncode+= EncodeTable[((Tmp[ 2] <<  2) | (Tmp[ 3] >>  6)) &  0x3F];
        strEncode+= EncodeTable[Tmp[ 3] &  0x3F];
         if(LineLength+= 4,LineLength== 76) {strEncode+= " \r\n ";LineLength= 0;}
    }
     // 对剩余数据进行编码
     int Mod=DataByte %  3;
     if(Mod== 1)
    {
        Tmp[ 1] = *Data++;
        strEncode+= EncodeTable[(Tmp[ 1] &  0xFC) >>  2];
        strEncode+= EncodeTable[((Tmp[ 1] &  0x03) <<  4)];
        strEncode+=  " == ";
    }
     else  if(Mod== 2)
    {
        Tmp[ 1] = *Data++;
        Tmp[ 2] = *Data++;
        strEncode+= EncodeTable[(Tmp[ 1] &  0xFC) >>  2];
        strEncode+= EncodeTable[((Tmp[ 1] &  0x03) <<  4) | ((Tmp[ 2] &  0xF0) >>  4)];
        strEncode+= EncodeTable[((Tmp[ 2] &  0x0F) <<  2)];
        strEncode+=  " = ";
    }
    
     return strEncode;
}

string ZBase64::Decode( const  char* Data, int DataByte, int& OutByte)
{
     // 解码表
     const  char DecodeTable[] =
    {
         000000000000000000000000,
         0000000000000000000,
         62//  '+'
         000,
         63//  '/'
         52535455565758596061//  '0'-'9'
         0000000,
         0123456789101112,
         13141516171819202122232425//  'A'-'Z'
         000000,
         26272829303132333435363738,
         39404142434445464748495051//  'a'-'z'
    };
     // 返回值
     string strDecode;
     int nValue;
     int i=  0;
     while (i < DataByte)
    {
         if (*Data !=  ' \r ' && *Data!= ' \n ')
        {
            nValue = DecodeTable[*Data++] <<  18;
            nValue += DecodeTable[*Data++] <<  12;
            strDecode+=(nValue &  0x00FF0000) >>  16;
            OutByte++;
             if (*Data !=  ' = ')
            {
                nValue += DecodeTable[*Data++] <<  6;
                strDecode+=(nValue &  0x0000FF00) >>  8;
                OutByte++;
                 if (*Data !=  ' = ')
                {
                    nValue += DecodeTable[*Data++];
                    strDecode+=nValue &  0x000000FF;
                    OutByte++;
                }
            }
            i +=  4;
        }
         else //  回车换行,跳过
        {
            Data++;
            i++;
        }
     }
     return strDecode;
}
复制代码

使用示例(结合CxImage库):

复制代码
CString CScanDlg::EncodeImage()
{ // 对图片进行Base64编码
    ZBase64 zBase;
     // 图片编码
    CxImage  image;    //  定义一个CxImage对象    
    image.Load( this->m_strImgPath, CXIMAGE_FORMAT_JPG);    // 先装载jpg文件,需要指定文件类型
     long size= 0; // 得到图像大小
    BYTE* buffer= 0; // 存储图像数据的缓冲
    image.Encode(buffer,size,CXIMAGE_FORMAT_JPG); // 把image对象中的图像以type类型数据copy到buffer
     string strTmpResult=zBase.Encode(buffer,size);
    CString result;
    result = strTmpResult.c_str();
     return result;
}
复制代码

复制代码
void CScanDlg::DecodeImageData(CString strData)
{ // 对Base64编码过的数据解码并显示原图片

    ZBase64 zBase;
     int OutByte= 0;
     string strTmpResult=zBase.Decode(strData,strData.GetLength(),OutByte);
     int i,len = strTmpResult.length();
    BYTE *buffer =  new BYTE[len];
     for (i= 0;i<len;++i)
    {
        buffer[i] = strTmpResult[i];
    }
    CxImage image(buffer,len,CXIMAGE_FORMAT_JPG); // 把内存缓冲buffer中的数据构造成Image对象
    delete [] buffer;
    CDC* hdc = m_picture.GetDC();
    m_bitmap = image.MakeBitmap(hdc->m_hDC);
    HBITMAP h0ldBmp = m_picture.SetBitmap(m_bitmap);
     if(h0ldBmp) DeleteObject(h0ldBmp);
     if(hdc->m_hDC) m_picture.ReleaseDC(hdc);
     if(m_bitmap) DeleteObject(m_bitmap);
}

复制代码

作者:洞庭散人

出处:http://phinecos.cnblogs.com/    

本博客遵从 Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++中实现base64编解码的示例代码: ```cpp #include <iostream> #include <string> #include <cstring> using namespace std; const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; string base64_encode(const char* bytes_to_encode, unsigned int in_len) { string ret; int i = 0, j = 0; unsigned char char_array_3[3], char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (i = 0; (i < 4); i++) { ret += base64_chars[char_array_4[i]]; } i = 0; } } if (i) { for (j = i; j < 3; j++) { char_array_3[j] = '\0'; } char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) { ret += base64_chars[char_array_4[j]]; } while ((i++ < 3)) { ret += '='; } } return ret; } string base64_decode(string const& encoded_string) { int in_len = encoded_string.size(); int i = 0, j = 0, in_ = 0; unsigned char char_array_4[4], char_array_3[3]; string ret; while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i == 4) { for (i = 0; i < 4; i++) { char_array_4[i] = base64_chars.find(char_array_4[i]); } char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) { ret += char_array_3[i]; } i = 0; } } if (i) { for (j = i; j < 4; j++) { char_array_4[j] = 0; } for (j = 0; j < 4; j++) { char_array_4[j] = base64_chars.find(char_array_4[j]); } char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) { ret += char_array_3[j]; } } return ret; } int main() { string str = "Hello, world!"; string encoded = base64_encode(str.c_str(), str.length()); cout << "Encoded string: " << encoded << endl; string decoded = base64_decode(encoded); cout << "Decoded string: " << decoded << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值