3

我目前正在处理使用 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

所以问题是,有没有人有使用这些功能的经验并看到不正确的地方,或者对于这个字符的转换假设有什么不正确的地方吗?

4

1 回答 1

0

您需要在调用之前重置resultCode为。从手册中引用:U_ZERO_ERRORucnv_open

“ICU 函数采用 UErrorCode 的引用 (C++) 或指针 (C) 首先测试 if(U_FAILURE(errorCode)) { 立即返回;} 以便在此类函数链中,第一个设置错误代码的函数会导致以下不执行任何操作”

于 2014-03-05T22:05:41.497 回答