gpt4 book ai didi

c++ - 如何将 UTF-8 编码的 std::string 转换为 UTF-16 std::string

转载 作者:搜寻专家 更新时间:2023-10-31 01:07:46 26 4
gpt4 key购买 nike

如何将 UTF-8 编码的 std::string 转换为 UTF-16 std::string?可能吗?

不,我不能在我的情况下使用 std::wstring。

Windows,MSVC-11.0。

最佳答案

试试这样怎么样:-

std::string s = u8"Your string";

// #include <codecvt>
std::wstring_convert<std::codecvt<char16_t,char,std::mbstate_t>,char16_t> convert;

std::u16string u16 = convert.from_bytes(s);
std::string u8 = convert.to_bytes(u16);

同时检查 this用于 UTF 到 UTF 的转换。

来自文档:-

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.

关于c++ - 如何将 UTF-8 编码的 std::string 转换为 UTF-16 std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com