<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->#include<string>
usingnamespacestd;
classZBase64
{
public:
/*编码
DataByte
[in]输入的数据长度,以字节为单位
*/
stringEncode(constunsignedchar*Data,intDataByte);
/*解码
DataByte
[in]输入的数据长度,以字节为单位
OutByte
[out]输出的数据长度,以字节为单位,请不要通过返回值计算
输出数据的长度
*/
stringDecode(constchar*Data,intDataByte,int&OutByte);
};
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->#include"stdAfx.h"
#include"ZBase64.h"
stringZBase64::Encode(constunsignedchar*Data,intDataByte)
{
//编码表
constcharEncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//返回值
stringstrEncode;
unsignedcharTmp[4]={0};
intLineLength=0;
for(inti=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;}
}
//对剩余数据进行编码
intMod=DataByte%3;
if(Mod==1)
{
Tmp[1]=*Data++;
strEncode+=EncodeTable[(Tmp[1]&0xFC)>>2];
strEncode+=EncodeTable[((Tmp[1]&0x03)<<4)];
strEncode+="==";
}
elseif(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+="=";
}
returnstrEncode;
}
stringZBase64::Decode(constchar*Data,intDataByte,int&OutByte)
{
//解码表
constcharDecodeTable[]=
{
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'
};
//返回值
stringstrDecode;
intnValue;
inti=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++;
}
}
returnstrDecode;
}
使用示例(结合CxImage库):
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->CStringCScanDlg::EncodeImage()
{//对图片进行Base64编码
ZBase64zBase;
//图片编码
CxImageimage;//定义一个CxImage对象
image.Load(this->m_strImgPath,CXIMAGE_FORMAT_JPG);//先装载jpg文件,需要指定文件类型
longsize=0;//得到图像大小
BYTE*buffer=0;//存储图像数据的缓冲
image.Encode(buffer,size,CXIMAGE_FORMAT_JPG);//把image对象中的图像以type类型数据copy到buffer
stringstrTmpResult=zBase.Encode(buffer,size);
CStringresult;
result=strTmpResult.c_str();
returnresult;
}
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->voidCScanDlg::DecodeImageData(CStringstrData)
{//对Base64编码过的数据解码并显示原图片
ZBase64zBase;
intOutByte=0;
stringstrTmpResult=zBase.Decode(strData,strData.GetLength(),OutByte);
inti,len=strTmpResult.length();
BYTE*buffer=newBYTE[len];
for(i=0;i<len;++i)
{
buffer[i]=strTmpResult[i];
}
CxImageimage(buffer,len,CXIMAGE_FORMAT_JPG);//把内存缓冲buffer中的数据构造成Image对象
delete[]buffer;
CDC*hdc=m_picture.GetDC();
m_bitmap=image.MakeBitmap(hdc->m_hDC);
HBITMAPh0ldBmp=m_picture.SetBitmap(m_bitmap);
if(h0ldBmp)DeleteObject(h0ldBmp);
if(hdc->m_hDC)m_picture.ReleaseDC(hdc);
if(m_bitmap)DeleteObject(m_bitmap);
}
分享到:
相关推荐
Base64编解码 C++实现 已经测试过可以用 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法
C/C++ Base64编解码开源库,第三方开源库,亲测可用;
自己用C++写的base64编解码程序,含测试程序
vs08 base64编解码 加密 解密 c++ 源代码 可运行 注释详细
base64编码和解码c++代码,从live555源码中移植并修改过来的
C++实现的base64编解码类,无其他依赖,兼容windows linux。 C++实现的base64编解码类,无其他依赖,兼容windows linux。
本例中将MAC转为6位 IP转为4位,再通过Base64编解码显示
linux系统c++实现的url和base64编解码 在webserver中会使用 自己实现的一个webserver,详见https://download.csdn.net/download/k117470154/10234299
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法 编译器版本:VS2015 包括Base64和Base64URL编解码的C++代码实现
C++ STL编写的BASE64编解码源码 代码简洁易读 非常好用
Base64编解码库(源码)--C++,一个老外写的。
base64编解码C/C++实现。 .h和.cpp文件均已经上传,请直接调用压缩和解压缩方法即可,有需求的可私信我。
base64编解码 C++实现. static const char _B64_[64]={ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',...
使用该工具,可以把字节数组进行BASE64编码转成字符串,也可以把BASE64字串解码转成字节数组。
虽然JavaScript中可以使用原生的btoa和atob函数进行Base64的编解码。但是不支持中文字符,并且不支持url-safe的Base64编解码。当编码后的结果要是通过get请求传输时(比如跨域提交时),结果中包含有'/'字符将导致...
自己从网上找的资源,编译通过 其中有两种方法都实现了base64编解码
base64编码解码程序(包含从文件读取内容然后编码解码) 编码文件内容 解码文件内容 直接解码编码后的数据 直接编码给定明文
c++的base64编解码工具,支持二进制文件
使用C语言编写的base64加解密,可以在VC上使用