美文网首页
C++分割文件并进行BASE64编码的代码

C++分割文件并进行BASE64编码的代码

作者: jiangmm | 来源:发表于2019-04-23 10:12 被阅读0次

如下代码内容是关于C++分割文件并进行BASE64编码的代码。

#include <string>

using namespace std;

{

    const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    string strEncode;

    unsigned char Tmp[4]={0};

    for(long i=0;i<DataByte / 3;i++)

    {

        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];

    }

    int Mod=DataByte % 3;

    if(Mod==1)

    {

        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];

        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];

        strEncode+= "==";

    }

    else if(Mod==2)

    {

        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;

}

if(dlg.DoModal()==IDOK)

{

CFile m_splitFile;

if(m_splitFile.Open(dlg.GetPathName(),CFile::modeRead | CFile::shareDenyWrite))

{

CString targetPath="D:";

CString m_fileTitle=m_splitFile.GetFileName();

DWORD FileLength=m_splitFile.GetLength();

int nCount=1;

CString strName;

CFile wrFile;

DWORD ReadBytes;

while((ReadBytes=m_splitFile.Read(pBuf,PartLength))>=PartLength)

{

strName.Format("%s\%s%d.enc",targetPath,m_fileTitle,nCount++);

wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);

string encStr=Encode(pBuf,ReadBytes);

wrFile.Write(encStr.c_str(),encStr.size());

wrFile.Flush();

wrFile.Close();

}

if(nCount==1 && ReadBytes<PartLength)

{

strName.Format("%s\%s.enc",targetPath,m_fileTitle,nCount++);

wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);

string encStr=Encode(pBuf,ReadBytes);

wrFile.Write(encStr.c_str(),encStr.size());

wrFile.Flush();

wrFile.Close();

}

m_splitFile.Close();

delete pBuf;

}

}

相关文章

网友评论

      本文标题:C++分割文件并进行BASE64编码的代码

      本文链接:https://www.haomeiwen.com/subject/kwmmgqtx.html