阅读量:0
文章目录
概要
本文通过具体的代码示例,展示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,” 字符串的,要先截取掉,再解码。