Thi*_*unt 1 c++ string encoding std wstring
我从 HTTP 服务器响应接收正文字节,但我不知道如何将它们转换为 UTF8 字符串以使用它们。
我有一个想法,但我不确定它是否有效。我需要获取响应的字节并搜索它们并修改它们,因此我需要将std::vector<BYTE>tostd::wstring或std::string.
响应的 UTF8 字节编码在 my 中std::vector<BYTE>,如何将它们转换为std::string?我要不要把它们转换成std::wstring?。
我找到了这个代码:
std::string Encoding::StringToUtf8(const std::string& str)
{
INT size = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), NULL, 0);
std::wstring utf16_str(size, '\0');
MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), &utf16_str[0], size);
INT utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), NULL, 0, NULL, NULL);
std::string utf8_str(utf8_size, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), &utf8_str[0], utf8_size, NULL, NULL);
return utf8_str;
Run Code Online (Sandbox Code Playgroud)
}
但是现在,如果我想在字符串中搜索像“Ñ”这样的字符会起作用吗?,或者我是否要转换 a 中的字节std::wstring并搜索“Ñ”修改std::wstring并将其转换为std::string?
两者中的哪一个是正确的?
我需要将 UTF8 响应放在std::string或std::wstring中,以便搜索和修改数据(带有特殊字符)并将响应重新发送到 UTF8 中的客户端。
在 中存储 utf-8std::string只不过是在“向量”中存储字节序列。在std::string不知道任何编码任何东西的,任何成员函数类似find或<algorithm>功能一样std::find是行不通的,一旦你需要超越标准的ASCII工作。因此,您将如何处理这种情况取决于您,您可以尝试将输入 ( L"Ñ")转换为 utf-8 序列并尝试在其中找到它,std::string或者您可以将其转换string为wstring并直接对其进行处理。恕我直言,在您必须操作(搜索、提取单词、按字母拆分或替换以及所有这些超出 ASCII 范围)输入的情况下,您最好坚持wstring并在将其发布到客户端之前将其转换为 utf-8 std::string
EDIT001:截至std::codecvt_utf8上面在评论和我对性能问题的评论中提到。这是测试
std::wstring foo(const std::string& input)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(input.c_str());
}
std::wstring baz(const std::string& input)
{
std::wstring retVal;
auto targetSize = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), static_cast<int>(input.size()), NULL, 0);
retVal.resize(targetSize);
auto res = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), static_cast<int>(input.size()),
const_cast<LPWSTR>(retVal.data()), targetSize);
if(res == 0)
{
// handle error, throw, do something...
}
return retVal;
}
int main()
{
std::string input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut "
"labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
"voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat "
"cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
{
auto start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 100'000; ++i)
{
auto result = foo(input);
}
auto end = std::chrono::high_resolution_clock::now();
auto res = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Elapsed time: " << res << std::endl;
}
{
auto start = std::chrono::high_resolution_clock::now();
for(int i = 0; i < 100'000; ++i)
{
auto result = baz(input);
}
auto end = std::chrono::high_resolution_clock::now();
auto res = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Elapsed time: " << res << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并作为 Release x64 运行时的结果
经过时间:3065 经过时间:29
两个数量级...