static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string base64encode(char * a, int length)
{
unsigned int temp = 0;
std::string s;
for (int j = 0; j < length; j++){
int m = j % 3;
temp |= a[j]<<(2-m)*8;
if (j + 1 == length || m == 2)
{
for (int i = 0; i < m+2; i++){
s += base64Char[(temp >> (3 - i) * 6) & 0x3F];
}
temp = 0;
}
}
if (length%3 != 0)
for (int j = 0; j < 3-length % 3; j++){
s += *=*;
}
return s;
}
试了几次都正确,有什么办法验证这是100%准确的呢?
std::string base64encode(char * a, int length)
{
unsigned int temp = 0;
std::string s;
for (int j = 0; j < length; j++){
int m = j % 3;
temp |= a[j]<<(2-m)*8;
if (j + 1 == length || m == 2)
{
for (int i = 0; i < m+2; i++){
s += base64Char[(temp >> (3 - i) * 6) & 0x3F];
}
temp = 0;
}
}
if (length%3 != 0)
for (int j = 0; j < 3-length % 3; j++){
s += *=*;
}
return s;
}
试了几次都正确,有什么办法验证这是100%准确的呢?
