我是靠谱客的博主 英勇仙人掌,最近开发中收集的这篇文章主要介绍base64编码解码函数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

base64位编解码函数

void encode(byte* input ,byte*& output)
{
char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int len = strlen((char*)input);
int remain = len % 3;
if (remain == 0)
{
output = new byte[len / 3 * 4 + 1]; memset(output, 0, len / 3 * 4 + 1);
for (int i = 0, j = 0; i<len; i += 3, j += 4)
{
byte a1 = (input[i] >> 2) & 0x3f;
byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
output[j] = arr[a1];
output[j + 1] = arr[a2];
output[j + 2] = arr[a3];
output[j + 3] = arr[a4];
}
}
else if (remain == 1)
{
output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1);
int i = 0, j = 0;
for (; i<len - 3; i += 3, j += 4)
{
byte a1 = (input[i] >> 2) & 0x3f;
byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
output[j] = arr[a1];
output[j + 1] = arr[a2];
output[j + 2] = arr[a3];
output[j + 3] = arr[a4];
}
byte a1 = (input[i] >> 2) & 0x3f;
byte a2 = (((input[i] << 6) | 0x00) >> 2) & 0x3f;
output[j] = arr[a1];
output[j + 1] = arr[a2];
output[j + 2] = '=';
output[j + 3] = '=';
}
else if (remain == 2)
{
output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1);
int i = 0, j = 0;
for (; i<len - 3; i += 3, j += 4)
{
byte a1 = (input[i] >> 2) & 0x3f;
byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
output[j] = arr[a1];
output[j + 1] = arr[a2];
output[j + 2] = arr[a3];
output[j + 3] = arr[a4];
}
byte a1 = (input[i] >> 2) & 0x3f;
byte a2 = (((input[i] << 6) | input[i + 1] >> 4 << 2) >> 2) & 0x3f;
byte a3 = (((input[i + 1] << 4) | 0x00) >> 2) & 0x3f;
output[j] = arr[a1];
output[j + 1] = arr[a2];
output[j + 2] = arr[a3];
output[j + 3] = '=';
}
}
byte find(byte c)
{
char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (byte i = 0; i<64; i++)
{
if (arr[i] == c)
{
return i;
}
}
}
void decode(byte* input, byte*& output)
{
int len = strlen((char*)input);
if (input[len - 1] != '=')
{
output = new byte[len / 4 * 3 + 1];
memset(output, 0, len / 4 * 3 + 1);
for (int i = 0, j = 0; i<len; i += 4, j += 3)
{
byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
output[j] = a1;
output[j + 1] = a2;
output[j + 2] = a3;
}
}
else if (input[len - 1] == '='&&input[len - 2] == '=')
{
output = new byte[len / 4 * 3 - 2 + 1];
memset(output, 0, len / 4 * 3 - 2 + 1);
int i = 0, j = 0;
for (; i<len - 4; i += 4, j += 3)
{
byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
output[j] = a1;
output[j + 1] = a2;
output[j + 2] = a3;
}
byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
output[j] = a1;
}
else if (input[len - 1] == '='&&input[len - 2] != '=')
{
output = new byte[len / 4 * 3 - 1 + 1];
memset(output, 0, len / 4 * 3 - 1 + 1);
int i = 0, j = 0;
for (; i<len - 4; i += 4, j += 3)
{
byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
output[j] = a1;
output[j + 1] = a2;
output[j + 2] = a3;
}
byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
output[j] = a1;
output[j + 1] = a2;
}
}


最后

以上就是英勇仙人掌为你收集整理的base64编码解码函数的全部内容,希望文章能够帮你解决base64编码解码函数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部