问题
I have this code, which convert UTF-8 string to Unicode:
#include <unicode/unistr.h>
//included other header files
int main(int argc, char** argv) {
std::string s("some string");
// convert std::string to ICU's UnicodeString
UnicodeString ucs = UnicodeString::fromUTF8(StringPiece(s.c_str()));
// convert UnicodeString to std::wstring
std::wstring ws;
for (int i = 0; i < ucs.length(); ++i)
ws += static_cast<wchar_t>(ucs[i]);
std::wcout << ws;
}
I can not understand how to convert this UnicodeString to windows-1251 (cp1251). Which function shoud I use to do this in Linux ?
回答1:
Use ICU's conversion functions in ucnv.h
(see Conversion > Using Converters in ICU's documentation):
#include <memory>
#include <unicode/ucnv.h>
bool convertTo1251(std::vector<UChar> const & input, std::vector<char> & output)
{
UErrorCode status = U_ZERO_ERROR;
UConverter *pConvert = ucnv_open("windows-1251", &status);
if (status)
{
printf("Failed to obtain char set converter: %d\r\n", status);
return false;
}
std::shared_ptr<UConverter> cnv(pConvert, ucnv_close);
UChar const * pwszBegin = &input[0], *pwszEnd = pwszBegin + input.size();
output.resize(input.size());
char *pszBegin = &output[0], *pszEnd = pszBegin + input.size();
ucnv_fromUnicode(pConvert, &pszBegin, pszEnd, &pwszBegin, pwszEnd, nullptr, true, &status);
if (status)
{
// deal with error
return false;
}
return true;
}
来源:https://stackoverflow.com/questions/48154898/how-to-convert-unicodestring-to-windows-1251-using-icu-library-in-c-linux