Base64编码与解码

概要

本文通过具体的代码示例,展示Base64的编码和解码操作,从而为您的开发工作提供实用的参考。

整体架构流程

首先,以二进制模式读取一个音频文件的内容,将其转换为Base64编码格式。接着,将Base64编码的数据解码回原始的二进制形式,从而还原出原始文件。

std::string Base64Encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
    std::string ret;
    int i = 0;
    int j = 0;
    unsigned char char_array_3[3];
    unsigned char char_array_4[4];

    while (in_len--) {
        char_array_3[i++] = *(bytes_to_encode++);
        if (i == 3) {
            char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
            char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
            char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
            char_array_4[3] = char_array_3[2] & 0x3f;

            for (i = 0; (i < 4); i++) ret += base64_chars[char_array_4[i]];
            i = 0;
        }
    }

    if (i) {
        for (j = i; j < 3; j++) char_array_3[j] = '\0';

        char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
        char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
        char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
        char_array_4[3] = char_array_3[2] & 0x3f;

        for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]];

        while ((i++ < 3)) ret += '=';
    }

    return ret;
}

std::string Base64Decode(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'
    };
    // 返回值
    std::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() {
    // 以二进制模式读取文件
    ifstream file("tmp.pcm", std::ios::binary | std::ios::ate);
    streampos fileSize = file.tellg();
    file.seekg(0, std::ios::beg);
    vector<char> fileData(fileSize);
    file.read(fileData.data(), fileSize);
    file.close();
    // 编码
    string fileBase64 = Base64Encode(reinterpret_cast<const unsigned char*>(fileData.data()), fileData.size());
    // 解码并输出原文件(有时候从外部获取来的 base64 数据,开头是带有 "base64," 字符串的,要先截取掉,再解码)
    int outByte = 0;
    string strResult = Base64Decode(fileBase64.c_str(), fileBase64.length(), outByte);
    unsigned char *buffer = new unsigned char[outByte];
    for (int i = 0; i < outByte; ++i) {
        buffer[i] = strResult[i];
    }
    // 保存文件数据
    std::ofstream fAudioFile;
    fAudioFile.open("tmp_new.pcm", std::ios::out | std::ios::binary | std::ios::trunc);
    fAudioFile.write((char *)buffer, outByte);
    fAudioFile.close();
    delete[] buffer;
}

技术细节

有时候从外部获取来的 base64 数据,开头是带有 “base64,” 字符串的,要先截取掉,再解码。

相关推荐

  1. Base64编码解码

    2024-07-20 10:40:04       29 阅读
  2. 图片based64编码解码python代码

    2024-07-20 10:40:04       33 阅读
  3. 图片Base64编码

    2024-07-20 10:40:04       37 阅读
  4. php的Url 安全的base64编码解码

    2024-07-20 10:40:04       57 阅读
  5. Base64 编码表 参考

    2024-07-20 10:40:04       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 10:40:04       101 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 10:40:04       109 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 10:40:04       87 阅读
  4. Python语言-面向对象

    2024-07-20 10:40:04       96 阅读

热门阅读

  1. Android Studio关于Gradle及JDK问题解决

    2024-07-20 10:40:04       24 阅读
  2. Oracle(12)什么是主键(Primary Key)?

    2024-07-20 10:40:04       26 阅读
  3. 目标检测算法

    2024-07-20 10:40:04       24 阅读
  4. 使用python调用dll库

    2024-07-20 10:40:04       23 阅读
  5. 数据结构之栈、队列和数组的基本概念

    2024-07-20 10:40:04       23 阅读
  6. RoCE(RDMA over Converged Ethernet)网络速率测试工具

    2024-07-20 10:40:04       24 阅读
  7. 读取 Excel 文件

    2024-07-20 10:40:04       20 阅读
  8. 实战:springboot用LocalDateTime快速替换Date

    2024-07-20 10:40:04       18 阅读