Base64编解码(C++版)
生活随笔
收集整理的這篇文章主要介紹了
Base64编解码(C++版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include?<string>
using?namespace?std;
class?ZBase64
{
public:
????/*編碼
????DataByte
????????[in]輸入的數(shù)據(jù)長度,以字節(jié)為單位
????*/
????string?Encode(const?unsigned?char*?Data,int?DataByte);
????/*解碼
????DataByte
????????[in]輸入的數(shù)據(jù)長度,以字節(jié)為單位
????OutByte
????????[out]輸出的數(shù)據(jù)長度,以字節(jié)為單位,請不要通過返回值計(jì)算
????????輸出數(shù)據(jù)的長度
????*/
????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;}
????}
????//對剩余數(shù)據(jù)進(jìn)行編碼
????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[]?=
????{
????????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'
????};
????//返回值
????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;
}
{//對圖片進(jìn)行Base64編碼
????ZBase64?zBase;
????//圖片編碼
????CxImage??image;???//?定義一個(gè)CxImage對象????
????image.Load(this->m_strImgPath,?CXIMAGE_FORMAT_JPG);???//先裝載jpg文件,需要指定文件類型
????long?size=0;//得到圖像大小
????BYTE*?buffer=0;//存儲(chǔ)圖像數(shù)據(jù)的緩沖
????image.Encode(buffer,size,CXIMAGE_FORMAT_JPG);//把image對象中的圖像以type類型數(shù)據(jù)copy到buffer
????string?strTmpResult=zBase.Encode(buffer,size);
????CString?result;
????result?=?strTmpResult.c_str();
????return?result;
}
{//對Base64編碼過的數(shù)據(jù)解碼并顯示原圖片
????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);//把內(nèi)存緩沖buffer中的數(shù)據(jù)構(gòu)造成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);
}
using?namespace?std;
class?ZBase64
{
public:
????/*編碼
????DataByte
????????[in]輸入的數(shù)據(jù)長度,以字節(jié)為單位
????*/
????string?Encode(const?unsigned?char*?Data,int?DataByte);
????/*解碼
????DataByte
????????[in]輸入的數(shù)據(jù)長度,以字節(jié)為單位
????OutByte
????????[out]輸出的數(shù)據(jù)長度,以字節(jié)為單位,請不要通過返回值計(jì)算
????????輸出數(shù)據(jù)的長度
????*/
????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;}
????}
????//對剩余數(shù)據(jù)進(jìn)行編碼
????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[]?=
????{
????????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'
????};
????//返回值
????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;
}
使用示例(結(jié)合CxImage庫):
{//對圖片進(jìn)行Base64編碼
????ZBase64?zBase;
????//圖片編碼
????CxImage??image;???//?定義一個(gè)CxImage對象????
????image.Load(this->m_strImgPath,?CXIMAGE_FORMAT_JPG);???//先裝載jpg文件,需要指定文件類型
????long?size=0;//得到圖像大小
????BYTE*?buffer=0;//存儲(chǔ)圖像數(shù)據(jù)的緩沖
????image.Encode(buffer,size,CXIMAGE_FORMAT_JPG);//把image對象中的圖像以type類型數(shù)據(jù)copy到buffer
????string?strTmpResult=zBase.Encode(buffer,size);
????CString?result;
????result?=?strTmpResult.c_str();
????return?result;
}
void?CScanDlg::DecodeImageData(CString?strData)
{//對Base64編碼過的數(shù)據(jù)解碼并顯示原圖片
????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);//把內(nèi)存緩沖buffer中的數(shù)據(jù)構(gòu)造成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);
}
總結(jié)
以上是生活随笔為你收集整理的Base64编解码(C++版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web性能测试分析-工具篇 (转载)
- 下一篇: matlab c++ 符号变量问题