在输入字符串中转换 r n的C 将 r n转换为运输返回和新线



我需要转换一个输入字符串,该字符串可以包含 r n作为字符串的一部分。例如,如下

给出
rn Content-type: text/plain; charset=iso-8859-1 rn Subject: Test Subject rnrn Test Message 

现在,在通过HTTP帖子数据发送此字符串时,我需要将 r n转换为百分位编码。但是,当我使用curl_easy_escape函数时,它将 r n理解为不同的字符,并错误地编码。因此,为避免这种错误,我需要在上面的字符串中将 r n转换为马车返回和newline,以便缓冲区通过curl_easy_escape()函数正确转换。我尝试使用缓冲区使用SStream对象,Sprintf和Sscanf(因为缓冲区是一个std :: String对象),但没有太大帮助。基本上我想将缓冲区转换为以下

content-type:text/plain;charset = ISO-8859-1 主题:测试主题

测试消息

这样,当我们将此缓冲区传递给curl_easy_escape时,它会正确编码。因此,在这方面的任何指针都会非常有帮助。

您可以在循环中使用 std::string::findstd::string::replace进行:

std::string input = "\r\n Content-type: text/plain; charset=iso-8859-1 \r\n Subject: Test Subject \r\n\r\n Test Message";
std::string::size_type pos = 0;
while ((pos = input.find("\r\n", pos)) != std::string::npos)
{
    input.replace(pos, 4, "rn");
}

如果您可以访问<regex>成员:

std::string const input("abcd\r\nefgh\r\nijkl");
std::string const output(std::regex_replace(input, std::regex("\\r\\n"), std::string("rn")));

最新更新