C++:如何在不丢失 NUL 字符的情况下将 std::string 编码为 base64
C++: How to encode a std::string into base64 without losing NUL character
我正在努力 someone else's code 和 运行。代码是用 C++ 编写的。失败的部分是将 std::string 转换为 base64:
std::string tmp = "[=11=]";
tmp.append(strUserName);
tmp.append("[=11=]");
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());
其中 base64 是:
std::string base64_encode(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] = '[=12=]';
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;
}
它使用 'tmp' 字符串调用服务器,并且必须在 base64 字符串中嵌入两个 NUL 字符(在 strUserName 之前和 strPassword 之前)。然而,似乎由于代码将 tmp 作为 c_str() 传递,NUL 字符被剥离。对此有好的解决方案吗?谢谢。
Update 我想我应该补充一点,代码包括“#include <asm/errno.h>
”,我用谷歌搜索但没有找到与 macOS 的兼容性,所以我把它注释掉了.. 不确定这是否会使事情无法正常工作,但我对此表示怀疑。全面披露。
std::string tmp = "[=11=]";
和 tmp.append("[=12=]");
不向 tmp
添加任何 '[=13=]'
个字符。采用 const char*
的 std::string::string
和 std::string::append
版本采用以 NUL 结尾的 C 风格字符串,因此它们一看到 NUL 字符就会停止。
要实际将 NUL 字符添加到您的字符串中,您需要使用构造函数和 append
方法来获取长度以及 const char*
,或者使用计数的版本和一个 char
:
std::string tmp("[=10=]", 1);
tmp.append(strUserName);
tmp.append("[=10=]", 1);
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());
我正在努力 someone else's code 和 运行。代码是用 C++ 编写的。失败的部分是将 std::string 转换为 base64:
std::string tmp = "[=11=]";
tmp.append(strUserName);
tmp.append("[=11=]");
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());
其中 base64 是:
std::string base64_encode(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] = '[=12=]';
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;
}
它使用 'tmp' 字符串调用服务器,并且必须在 base64 字符串中嵌入两个 NUL 字符(在 strUserName 之前和 strPassword 之前)。然而,似乎由于代码将 tmp 作为 c_str() 传递,NUL 字符被剥离。对此有好的解决方案吗?谢谢。
Update 我想我应该补充一点,代码包括“#include <asm/errno.h>
”,我用谷歌搜索但没有找到与 macOS 的兼容性,所以我把它注释掉了.. 不确定这是否会使事情无法正常工作,但我对此表示怀疑。全面披露。
std::string tmp = "[=11=]";
和 tmp.append("[=12=]");
不向 tmp
添加任何 '[=13=]'
个字符。采用 const char*
的 std::string::string
和 std::string::append
版本采用以 NUL 结尾的 C 风格字符串,因此它们一看到 NUL 字符就会停止。
要实际将 NUL 字符添加到您的字符串中,您需要使用构造函数和 append
方法来获取长度以及 const char*
,或者使用计数的版本和一个 char
:
std::string tmp("[=10=]", 1);
tmp.append(strUserName);
tmp.append("[=10=]", 1);
tmp.append(strPassword);
tmp = base64_encode(tmp.c_str(), tmp.length());