How to convert UnicodeString to windows-1251 using ICU library in c++ Linux?

梦想与她 提交于 2020-01-03 03:24:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!