C++二进制字符串转Base64字符串 Base64字符串转二进制字符串

时间:2024-01-18 19:44:50

封装成类的 。 base64格式的字符串,只包含大小写字母、零到九,以及 + /

//___base_64.h

/*base_64.h文件*/
#ifndef BASE_64_H
#define BASE_64_H
/**
* Base64 编码/解码
* @author liruixing
*/
class Base64{
private:
std::string _base64_table;
static const char base64_pad = '=';public:
Base64()
{
_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /*这是Base64编码使用的标准字典*/
}
/**
* 这里必须是unsigned类型,否则编码中文的时候出错
*/
std::string Encode(const unsigned char * str,int bytes);
std::string Decode(const char *str,int bytes);
void Debug(bool open = true);
};
#endif
//___base_64.cpp
/*base_64.cpp文件*/
#include <iostream>
#include <string>
#include <cstring>
#include "base_64.h" std::string Base64::Encode(const unsigned char * str,int bytes) {
int num = ,bin = ,i;
std::string _encode_result;
const unsigned char * current;
current = str;
while(bytes > ) {
_encode_result += _base64_table[current[] >> ];
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[((current[] & 0x0f) << ) + (current[] >> )];
_encode_result += _base64_table[current[] & 0x3f]; current += ;
bytes -= ;
}
if(bytes > )
{
_encode_result += _base64_table[current[] >> ];
if(bytes% == ) {
_encode_result += _base64_table[(current[] & 0x03) << ];
_encode_result += "==";
} else if(bytes% == ) {
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[(current[] & 0x0f) << ];
_encode_result += "=";
}
}
return _encode_result;
}
std::string Base64::Decode(const char *str,int length) {
//解码表
const char DecodeTable[] =
{
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -
};
int bin = ,i=,pos=;
std::string _decode_result;
const char *current = str;
char ch;
while( (ch = *current++) != '\0' && length-- > )
{
if (ch == base64_pad) { // 当前一个字符是“=”号
/*
先说明一个概念:在解码时,4个字符为一组进行一轮字符匹配。
两个条件:
1、如果某一轮匹配的第二个是“=”且第三个字符不是“=”,说明这个带解析字符串不合法,直接返回空
2、如果当前“=”不是第二个字符,且后面的字符只包含空白符,则说明这个这个条件合法,可以继续。
*/
if (*current != '=' && (i % ) == ) {
return NULL;
}
continue;
}
ch = DecodeTable[ch];
//这个很重要,用来过滤所有不合法的字符
if (ch < ) { /* a space or some other separator character, we simply skip over */
continue;
}
switch(i % )
{
case :
bin = ch << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x0f ) << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x03 ) << ;
break;
case :
bin |= ch;
_decode_result += bin;
break;
}
i++;
}
return _decode_result;
}

附1:C++实现 safaBase64格式跟nonSafeBase64格式的转换

附2:C++二进制字符串转十六进制字符串 十六进制字符串转二进制字符串