UTF8 与 STL 中的宽字符转换

UTF8 to/from wide char conversion in STL(UTF8 与 STL 中的宽字符转换)

本文介绍了UTF8 与 STL 中的宽字符转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以以独立于平台的方式将 std::string 中的 UTF8 字符串转换为 std::wstring,反之亦然?在 Windows 应用程序中,我将使用 MultiByteToWideChar 和 WideCharToMultiByte.但是,代码是为多个操作系统编译的,我仅限于标准 C++ 库.

Is it possible to convert UTF8 string in a std::string to std::wstring and vice versa in a platform independent manner? In a Windows application I would use MultiByteToWideChar and WideCharToMultiByte. However, the code is compiled for multiple OSes and I'm limited to standard C++ library.

推荐答案

我 5 年前问过这个问题.这个帖子当时对我很有帮助,我得出了一个结论,然后我继续我的项目.有趣的是,我最近需要类似的东西,与过去的那个项目完全无关.在研究可能的解决方案时,我偶然发现了自己的问题:)

I've asked this question 5 years ago. This thread was very helpful for me back then, I came to a conclusion, then I moved on with my project. It is funny that I needed something similar recently, totally unrelated to that project from the past. As I was researching for possible solutions, I stumbled upon my own question :)

我现在选择的解决方案是基于C++11.康斯坦丁在他的回答中提到的 boost 库现在是标准的一部分.如果我们用新的字符串类型 std::u16string 替换 std::wstring,那么转换将如下所示:

The solution I chose now is based on C++11. The boost libraries that Constantin mentions in his answer are now part of the standard. If we replace std::wstring with the new string type std::u16string, then the conversions will look like this:

UTF-8 到 UTF-16

std::string source;
...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string dest = convert.from_bytes(source);    

UTF-16 到 UTF-8

std::u16string source;
...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::string dest = convert.to_bytes(source);    

从其他答案中可以看出,该问题有多种方法.这就是为什么我不选择可接受的答案.

As seen from the other answers, there are multiple approaches to the problem. That's why I refrain from picking an accepted answer.

这篇关于UTF8 与 STL 中的宽字符转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UTF8 与 STL 中的宽字符转换

基础教程推荐