深入解析Base64编码原理与实现,R语言 表达矩阵 count_table 筛选出 行名是 某个 基因的 数据或者某个列中的数据是某个基因的数据。
·
Base64 编码原理
Base64 是一种基于 64 个可打印字符表示二进制数据的编码方式。它将每 3 字节(24 位)的二进制数据分为 4 组,每组 6 位,对应一个 Base64 字符。若原始数据不足 3 字节,会用 = 填充。
编码表包含 A-Z、a-z、0-9 以及 + 和 / 共 64 个字符。例如,十六进制 0x4D 0x61 0x6E 编码后为 TWFu。
Base64 编码步骤
- 将输入数据按 3 字节分组,最后一组不足 3 字节时补零。
- 将每组 24 位拆分为 4 个 6 位的段。
- 每个 6 位段转换为对应的 Base64 字符。
- 若最后有补零,用
=替换对应的输出字符。
C++ 实现编码
#include <string>
#include <vector>
const std::string BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64_encode(const std::vector<uint8_t>& data) {
std::string encoded;
int i = 0;
int j = 0;
uint8_t byte_array_3[3];
uint8_t byte_array_4[4];
for (auto byte : data) {
byte_array_3[i++] = byte;
if (i == 3) {
byte_array_4[0] = (byte_array_3[0] & 0xfc) >> 2;
byte_array_4[1] = ((byte_array_3[0] & 0x03) << 4) + ((byte_array_3[1] & 0xf0) >> 4);
byte_array_4[2] = ((byte_array_3[1] & 0x0f) << 2) + ((byte_array_3[2] & 0xc0) >> 6);
byte_array_4[3] = byte_array_3[2] & 0x3f;
for (i = 0; i < 4; i++) {
encoded += BASE64_CHARS[byte_array_4[i]];
}
i = 0;
}
}
if (i > 0) {
for (j = i; j < 3; j++) {
byte_array_3[j] = 0;
}
byte_array_4[0] = (byte_array_3[0] & 0xfc) >> 2;
byte_array_4[1] = ((byte_array_3[0] & 0x03) << 4) + ((byte_array_3[1] & 0xf0) >> 4);
byte_array_4[2] = ((byte_array_3[1] & 0x0f) << 2) + ((byte_array_3[2] & 0xc0) >> 6);
byte_array_4[3] = byte_array_3[2] & 0x3f;
for (j = 0; j < i + 1; j++) {
encoded += BASE64_CHARS[byte_array_4[j]];
}
while (i++ < 3) {
encoded += '=';
}
}
return encoded;
}
Base64 解码原理
解码是编码的逆过程:
- 移除填充的
=并计算原始数据长度。 - 每个 Base64 字符转换为对应的 6 位值。
- 将 4 个 6 位值组合为 3 个 8 位字节。
C++ 实现解码
#include <algorithm>
std::vector<uint8_t> base64_decode(const std::string& encoded) {
std::vector<uint8_t> decoded;
int i = 0;
int j = 0;
int in_len = encoded.size();
uint8_t byte_array_3[3];
uint8_t byte_array_4[4];
for (int k = 0; k < in_len; k++) {
if (encoded[k] == '=') {
byte_array_4[i++] = 0;
} else {
size_t pos = BASE64_CHARS.find(encoded[k]);
if (pos != std::string::npos) {
byte_array_4[i++] = pos;
}
}
if (i == 4) {
byte_array_3[0] = (byte_array_4[0] << 2) + ((byte_array_4[1] & 0x30) >> 4);
byte_array_3[1] = ((byte_array_4[1] & 0x0f) << 4) + ((byte_array_4[2] & 0x3c) >> 2);
byte_array_3[2] = ((byte_array_4[2] & 0x03) << 6) + byte_array_4[3];
for (i = 0; i < 3; i++) {
decoded.push_back(byte_array_3[i]);
}
i = 0;
}
}
if (i > 0) {
for (j = i; j < 4; j++) {
byte_array_4[j] = 0;
}
byte_array_3[0] = (byte_array_4[0] << 2) + ((byte_array_4[1] & 0x30) >> 4);
byte_array_3[1] = ((byte_array_4[1] & 0x0f) << 4) + ((byte_array_4[2] & 0x3c) >> 2);
byte_array_3[2] = ((byte_array_4[2] & 0x03) << 6) + byte_array_4[3];
for (j = 0; j < i - 1; j++) {
decoded.push_back(byte_array_3[j]);
}
}
return decoded;
}
应用场景
Base64 常用于:
- 在 XML、JSON 中嵌入二进制数据
- 电子邮件传输二进制附件
- HTTP Basic Authentication
- 数据校验(如 MD5/SHA 哈希值)
性能优化建议
- 使用查表法替代算术运算加速字符转换。
- 预分配输出缓冲区避免频繁内存分配。
- 使用 SIMD 指令并行处理多个字符(如 SSE/AVX)。
m.olgvj.cn
m.akkpa.cn
m.rjtgb.cn
m.wwztb.cn
m.doyvd.cn
更多推荐



所有评论(0)