I receive the body bytes from an HTTP server response and I dont know how to convert them to an UTF8 string to work with them.
I have an idea but I am not sure wheter it works. I need to get the bytes of the response and search on them and modify them, so I need to transform the std::vector<BYTE>
to std::wstring
or std::string
.
The bytes encoding in UTF8 of the response are in my std::vector<BYTE>
, how can I transform them to a std::string
? Shall I transform them to std::wstring
?.
I found this code:
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;
}
But now if I want to search a character like "Ñ" in the string will work?, or Have I to transform the bytes in a std::wstring
and search the "Ñ" modify the std::wstring
and convert it to std::string
?
Which of the two would be correct?
I need to put the UTF8 response in a std::string
or std::wstring
in order to search and modify the data (with special characters) and resend the response to the client in UTF8.