如何将UTF-8编码的std :: string转换为UTF-16 std :: string [英] How to convert UTF-8 encoded std::string to UTF-16 std::string

查看:1042
本文介绍了如何将UTF-8编码的std :: string转换为UTF-16 std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

不,我不能在我的case使用std :: wstring。

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

Windows,MSVC-11.0。

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);

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

Also check this for UTF to UTF conversion.

-


专业化codecvt在
之间转换UTF-16和UTF-8编码方案,以及专业化
codecvt在UTF-32和
UTF-8编码方案之间进行转换。

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.

这篇关于如何将UTF-8编码的std :: string转换为UTF-16 std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆