如何在c ++ Linux中使用ICU库将UnicodeString转换为windows-1251?

时间:2018-01-08 17:09:13

标签: c++ unicode icu unicode-string

我有这段代码,它将UTF-8字符串转换为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;
}

我无法理解如何将此UnicodeString转换为windows-1251(cp1251)。我用什么功能在Linux中执行此操作?

1 个答案:

答案 0 :(得分:2)

ucnv.h中使用ICU的转换功能(请参阅ICU文档中的Conversion > Using Converters):

#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;
}