How to iterate through unicode characters and print them on the screen with printf in C?

后端 未结 4 1251
不知归路
不知归路 2021-01-05 21:00

I want to iterate through all (at least the 16 bit) unicode characters and print them on the screen with C.

I know there are related questions on SO but they don\'t

4条回答
  •  半阙折子戏
    2021-01-05 21:43

    The function to convert a 16-bit Unicode codepoint to a multibyte character sequence is c16rtomb; there is also c32rtomb if you want to handle 32-bit codepoints:

    #include 
    
    mbstate_t ps;
    char buf[MB_CUR_MAX];
    size_t bytes = c16rtomb(buf, i, &ps);
    if (bytes != (size_t) -1) {
      printf("%.*s\n", bytes, buf);
    }
    

    If c16rtomb is not available you will need to use platform-specific facilities.

提交回复
热议问题