//base64.cpp
//
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
std::string base64encode(const char *infile,const char *outfile)
{
int DataByte=0;
unsigned char* Data=NULL;
FILE *fp=fopen(infile,"rb");
if(fp==NULL)
{
return "\0";
}
fseek(fp,0L,SEEK_END);
DataByte=ftell(fp);
Data=new unsigned char[DataByte+1];
fseek(fp,0L,SEEK_SET);
fread(Data,DataByte,1,fp);
fclose(fp);
//code table
const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//return value
std::string strEncode;
unsigned char Tmp[4]={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+=En
基于C++的Base64编解码实现
最新推荐文章于 2025-06-05 10:48:20 发布
文章介绍了如何使用C++编写Base64编码和解码函数,包括读取文件内容、处理字节、构建编码表并进行相应的转换。

6192

被折叠的 条评论
为什么被折叠?



