Comment convertir UnicodeSsortingng en windows-1251 en utilisant la bibliothèque ICU en c ++ Linux?

J’ai ce code, qui convertit la chaîne UTF-8 en Unicode:

#include  //included other header files int main(int argc, char** argv) { std::ssortingng s("some ssortingng"); // convert std::ssortingng to ICU's UnicodeSsortingng UnicodeSsortingng ucs = UnicodeSsortingng::fromUTF8(SsortingngPiece(s.c_str())); // convert UnicodeSsortingng to std::wssortingng std::wssortingng ws; for (int i = 0; i < ucs.length(); ++i) ws += static_cast(ucs[i]); std::wcout << ws; } 

Je ne peux pas comprendre comment convertir ce UnicodeSsortingng à Windows-1251 (cp1251). Quelle fonction dois-je utiliser pour faire cela sous Linux?

Utilisez les fonctions de conversion d’ICU dans ucnv.h (voir Conversion> Utilisation de convertisseurs dans la documentation d’ICU):

 #include  #include  bool convertTo1251(std::vector const & input, std::vector & 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 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; }