Chinaunix首页 | 论坛 | 博客
  • 博客访问: 357026
  • 博文数量: 132
  • 博客积分: 3066
  • 博客等级: 中校
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-14 16:19
文章分类

全部博文(132)

文章存档

2012年(1)

2010年(50)

2009年(81)

我的朋友

分类: C/C++

2009-05-25 12:33:31

#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;
}
 
使用示例(结合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;
}
 
 
阅读(939) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~