菜鸟笔记
提升您的技术认知

base64编解码(c 版)-ag真人游戏

.h文件:

#pragma once
#include 
using namespace std;
class zbase64
{
  
public:
	/*编码
	databyte
	[in]输入的数据长度,以字节为单位
	*/
	string encode(const unsigned char* data, int databyte);
	/*解码
	databyte
	[in]输入的数据长度,以字节为单位
	outbyte
	[out]输出的数据长度,以字节为单位,请不要通过返回值计算
	输出数据的长度
	*/
	string decode(const char* data, int databyte, int& outbyte);
};

.cpp文件:

#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; }
	}
	//对剩余数据进行编码
	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;
}

调用示例:

int main()
{
  
	zbase64 base64;
	int datalen(0);
	const unsigned char data[] = "hello zbase64";
	string imgbase64 = base64.encode(data,sizeof(data));
	cout << "imgbase64: " << imgbase64 << endl;
	string strcode = base64.decode(imgbase64.c_str(), imgbase64.size(), datalen);
	cout << "strcode: " << strcode << endl;
	return 0;
}

结果输出:

imgbase64: sgvsbg8gwkjhc2u2naa=
strcode: hello zbase64
网站地图