使用icu库进行UTF-8到UCS-2的转换

时间:2014-03-05 21:35:34

标签: c++ unicode utf-8 icu ucs2

我目前正在努力解决使用icu库将UTF-8字符串转换为UCS-2字符串的问题。在库中有很多方法可以做到这一点,但到目前为止它们似乎都没有工作,但考虑到这个库的流行,我假设我做错了。

首先是公共代码。在所有情况下,我都在一个对象上创建并传递一个字符串,但是在它到达转换步骤之前没有任何操作。

当前使用的utf-8字符串只是“ĩ”。

为简单起见,我将在此代码中表示用作uniString的字符串

UErrorCode resultCode = U_ZERO_ERROR;

UConverter* m_pConv = ucnv_open("ISO-8859-1", &resultCode);

// Change the callback to error out instead of the default            
const void* oldContext;
UConverterFromUCallback oldFromAction;
UConverterToUCallback oldToAction;
ucnv_setFromUCallBack(m_pConv, UCNV_FROM_U_CALLBACK_STOP, NULL, &oldFromAction, &oldContext, &resultCode);
ucnv_setToUCallBack(m_pConv, UCNV_TO_U_CALLBACK_STOP, NULL, &oldToAction, &oldContext, &resultCode);

int32_t outputLength = 0;
int bodySize = uniString.length();
int targetSize = bodySize * 4;
char* target = new char[targetSize];                       

printf("Body: %s\n", uniString.c_str());
if (U_SUCCESS(resultCode))
{
    // outputLength = ucnv_convert("ISO-8859-1", "UTF-8", target, targetSize, uniString.c_str(), bodySize, &resultCode);
    outputLength = ucnv_fromAlgorithmic(m_pConv, UCNV_UTF8, target, targetSize, uniString.c_str(),
        uniString.length(), &resultCode);
    ucnv_close(m_pConv);
}
printf("ISO-8859-1 DGF just tried to convert '%s' to '%s' with error '%i' and length '%i'", uniString.c_str(), 
    outputLength ? target : "invalid_char", resultCode, outputLength);

if (resultCode == U_INVALID_CHAR_FOUND || resultCode == U_ILLEGAL_CHAR_FOUND || resultCode == U_TRUNCATED_CHAR_FOUND)
{
    if (resultCode == U_INVALID_CHAR_FOUND)
    {
        printf("Unmapped input character, cannot be converted to Latin1");                    

        m_pConv = ucnv_open("UCS-2", &resultCode);
        if (U_SUCCESS(resultCode))
        {
            // outputLength = ucnv_convert("UCS-2", "UTF-8", target, targetSize, uniString.c_str(), bodySize, &resultCode);
            outputLength = ucnv_fromAlgorithmic(m_pConv, UCNV_UTF8, target, targetSize, uniString.c_str(),
                uniString.length(), &resultCode);
            ucnv_close(m_pConv);
        }

        printf("UCS-2 DGF just tried to convert '%s' to '%s' with error '%i' and length '%i'", uniString.c_str(), 
            outputLength ? target : "invalid_char", resultCode, outputLength);

        if (U_SUCCESS(resultCode))
        {
            pdus = SegmentText(target, pText, SEGMENT_SIZE_UNICODE_MAX, true);
        }
    }
    else
    {
        printf("DecodeText(): Text contents does not appear to be valid UTF-8");
    }
}
else
{
    printf("DecodeText(): Text successfully converted to Latin1");
    std::string newBody(target, outputLength);
    pdus = SegmentText(newBody, pPdu, SEGMENT_SIZE_MAX);
}

问题是ucnv_fromAlgorithmic函数为ucs-2转换引发了错误U_INVALID_CHAR_FOUND。这对于ISO-8859-1尝试有意义,但不适用于ucs-2。

另一种尝试是使用ucnv_convert,你可以看到它被注释掉了。此功能尝试转换,但在ISO-8859-1尝试时没有失败。

所以问题是,是否有人有这些功能的经验并且看到不正确的东西或者对于这个角色的转换假设有什么不正确的吗?

1 个答案:

答案 0 :(得分:0)

在致电resultCode之前,您需要将U_ZERO_ERROR重置为ucnv_open。引自manual

“ICU函数接受引用(C ++)或指针(C)到UErrorCode首先测试if(U_FAILURE(errorCode)){return immediately;}以便在这样的函数链中第一个设置一个错误代码导致以下错误代码不执行任何操作“