C library to convert unicode code points to UTF8?

前端 未结 4 976
我寻月下人不归
我寻月下人不归 2020-12-14 23:46

I have to go through some text and write UTF8 output according to the character patterns. I thought it\'ll be easy if I can work with the code points and get it converted to

4条回答
  •  粉色の甜心
    2020-12-15 00:29

    iconv could be used I figure.

    #include 
    
    iconv_t cd;
    char out[7];
    wchar_t in = CODE_POINT_VALUE;
    size_t inlen = sizeof(in), outlen = sizeof(out);
    
    cd = iconv_open("utf-8", "wchar_t");
    iconv(cd, (char **)&in, &inl, &out, &outlen);
    iconv_close(cd);
    

    But I fear that wchar_t might not represent Unicode code points, but arbitrary values.. EDIT: I guess you can do it by simply using a Unicode source:

    uint16_t in = UNICODE_POINT_VALUE;
    cd = iconv_open("utf-8", "ucs-2");
    

提交回复
热议问题