C++ UTF-8 output with ICU

后端 未结 3 1204
北恋
北恋 2021-01-11 11:57

I\'m struggling to get started with the C++ ICU library. I have tried to get the simplest example to work, but even that has failed. I would just like to output a UTF-8 stri

3条回答
  •  生来不讨喜
    2021-01-11 12:32

    Your program will work if you just change the initializer to:

    UnicodeString s("привет");
    

    The macro you were using is only for strings that contain "invariant characters", i.e., only latin letters, digits, and some punctuation.

    As was said before, input/output codepages are tricky. You said:

    My terminal and font support UTF-8 and I regularly use the terminal with UTF-8. My source code is in UTF-8.

    That may be true, but ICU doesn't know that's true. The process codepage might be different (let's say iso-8859-1), and the output codepage may be different (let's say shift-jis). Then, the program wouldn't work. But, the invariant characters using the API UNICODE_STRING_SIMPLE would still work.

    Hope this helps.

    srl, icu dev

提交回复
热议问题