UTF8 to/from wide char conversion in 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 中的宽字符转换
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17