【问题标题】:How to decode UTF-8 without having illegal input replaced by a replacement character?如何在不将非法输入替换为替换字符的情况下解码 UTF-8?
【发布时间】:2015-06-10 17:10:06
【问题描述】:

icu::UnicodeString::fromUTF8 将非法输入替换为 U+FFFD。有没有办法检测它是否已经这样做了,以便我可以抛出异常?

【问题讨论】:

    标签: c++ utf-8 icu


    【解决方案1】:

    使用 u_strFromUTF8

    UChar* u_strFromUTF8    (   UChar *     dest,
    int32_t     destCapacity,
    int32_t *   pDestLength,
    const char *    src,
    int32_t     srcLength,
    UErrorCode *    pErrorCode 
    )       
    Convert a UTF-8 string to UTF-16.
    

    如果输入字符串格式不正确,则 U_INVALID_CHAR_FOUND 错误码已设置

    http://icu-project.org/apiref/icu4c/ustring_8h.html#a5f9ff224b11166a106d1b3ac26454cd4

    【讨论】:

    • 很遗憾必须使用低级C API,但这是必须的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2013-01-29
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    相关资源
    最近更新 更多