Base64 Codec (c + + version)

Source: Internet
Author: User
Tags base64 base64 encode creative commons attribution

#include <string>
using namespace Std;

Class ZBase64
{
Public
/* Encoding
Databyte
The length of the data entered, in bytes
*/
String Encode (const unsigned char* data,int databyte);
/* Decode
Databyte
The length of the data entered, in bytes
Outbyte
The data length of the output, in bytes, should not be calculated by the return value
The length of the output data
*/
String Decode (const char* data,int databyte,int& outbyte);
};

#include "stdAfx.h"
#include "ZBase64.h"

String Zbase64::encode (const unsigned char* data,int databyte)
{
Encoding table
const char encodetable[]= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
return value
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;}
}
Encode the rest of the data
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::D ecode (const char* data,int databyte,int& outbyte)
{
Decode table
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,
--------------------------' A '-' Z '
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
--------------------------
};
return value
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//return to line, skip
{
data++;
i++;
}
}
return strdecode;
}

Using the example (in conjunction with the Cximage Library):

CString Cscandlg::encodeimage ()
{//Base64 encode a picture
ZBase64 zbase;
Image encoding
Cximage image; Define a Cximage Object
Image.   Load (This->m_strimgpath, cximage_format_jpg); To load the jpg file first, you need to specify the file type
Long size=0;//get the image size
byte* buffer=0;//Storage of image data buffering
Image. Encode (buffer,size,cximage_format_jpg);//Copy the images in the image object to buffer with type data
String Strtmpresult=zbase.encode (Buffer,size);
CString result;
result = Strtmpresult.c_str ();
return result;
}

void Cscandlg::D ecodeimagedata (CString strdata)
{//decode the BASE64 encoded data and display the original image

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);//To construct the data in buffer buffers into an image object
delete [] buffer;
cdc* hdc = m_picture. GetDC ();
M_bitmap = image. Makebitmap (HDC-&GT;M_HDC);
Hbitmap h0ldbmp = m_picture. SetBitmap (M_BITMAP);
if (h0ldbmp) DeleteObject (h0ldbmp);
if (HDC-&GT;M_HDC) m_picture. ReleaseDC (HDC);
if (m_bitmap) DeleteObject (M_BITMAP);
}

Cave Court scattered People

Source: http://phinecos.cnblogs.com/

This blog complies with the Creative Commons Attribution 3.0 License, which you may freely reprint for non-commercial purposes, but retain the original author information and the article link URL.

Base64 Codec (c + + version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.