编辑代码

#include <iostream>
using namespace std;

const string base64_chars = 
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
         "abcdefghijklmnopqrstuvwxyz"
         "0123456789+/";

void encode_char_array(unsigned char *encode_block, const unsigned char *decode_block) {
    encode_block[0] = (decode_block[0] & 0xfc) >> 2;
    encode_block[1] = ((decode_block[0] & 0x03) << 4) + ((decode_block[1] & 0xf0) >> 4);
    encode_block[2] = ((decode_block[1] & 0x0f) << 2) + ((decode_block[2] & 0xc0) >> 6);
    encode_block[3] = decode_block[2] & 0x3f;
}

string base64_encode(const string& input) {
    string output;
    size_t i = 0;
    unsigned char decode_block[3];
    unsigned char encode_block[4];

    for (string::size_type len = 0; len != input.size(); ++len) {
        decode_block[i++] = input[len];
        if (i == 3) {
            encode_char_array(encode_block, decode_block);
            for (i = 0; i < 4; ++i) {
                output += base64_chars[encode_block[i]];
            }
            i = 0;
        }
    }

    if (i > 0) {
        for (size_t j = i; j < 3; ++j) {
            decode_block[j] = '\0';
        }

        encode_char_array(encode_block, decode_block);

        for (size_t j = 0; j < i + 1; ++j) {
            output += base64_chars[encode_block[j]];
        }

        while (i++ < 3) {
            output += '=';
        }
    }

    return output;
}
int main() {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
	cout << base64_encode("你好abc")<< endl;
	return 0;
}