How to convert UTF-8 encoded std::string to UTF-16 std::string

前端 未结 2 915
半阙折子戏
半阙折子戏 2020-12-19 23:39

How can i convert UTF-8 encoded std::string to UTF-16 std::string? Is it possible?

And no, i can\'t use std::wstring in my case.

Windows, MSVC-11.0.

2条回答
  •  太阳男子
    2020-12-20 00:06

    How about trying like this:-

    std::string s = u8"Your string";
    
    // #include 
    std::wstring_convert,char16_t> convert;
    
    std::u16string u16 = convert.from_bytes(s);
    std::string u8 = convert.to_bytes(u16);
    

    Also check this for UTF to UTF conversion.

    From the docs:-

    The specialization codecvt converts between the UTF-16 and UTF-8 encoding schemes, and the specialization codecvt converts between the UTF-32 and UTF-8 encoding schemes.

提交回复
热议问题