C++ 将字符串(或 char*)转换为 wstring(或 wchar_t*) [英] C++ Convert string (or char*) to wstring (or wchar_t*)

查看:45
本文介绍了C++ 将字符串(或 char*)转换为 wstring(或 wchar_t*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string s = "おはよう";
wstring ws = FUNCTION(s, ws);

我如何将 s 的内容分配给 ws?

How would i assign the contents of s to ws?

搜索谷歌并使用了一些技术,但他们无法分配确切的内容.内容失真.

Searched google and used some techniques but they can't assign the exact content. The content is distorted.

推荐答案

假设您示例中的输入字符串 (おはよう) 是 UTF-8 编码的(从外观上看它不是,但让我们假设正是为了这个解释 :-)) 表示您感兴趣的 Unicode 字符串,那么您的问题可以单独使用标准库(C++11 和更新版本)完全解决.

Assuming that the input string in your example (おはよう) is a UTF-8 encoded (which it isn't, by the looks of it, but let's assume it is for the sake of this explanation :-)) representation of a Unicode string of your interest, then your problem can be fully solved with the standard library (C++11 and newer) alone.

TL;DR 版本:

#include <locale>
#include <codecvt>
#include <string>

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string narrow = converter.to_bytes(wide_utf16_source_string);
std::wstring wide = converter.from_bytes(narrow_utf8_source_string);

更长的在线可编译和可运行示例:

(他们都展示了相同的例子.只是有很多冗余......)

(They all show the same example. There are just many for redundancy...)

注意(旧):

正如评论中指出的那样,并在 https://stackoverflow.com/a/17106065/6345 中有解释在某些情况下,使用标准库在 UTF-8 和 UTF-16 之间进行转换可能会导致不同平台上的结果出现意外差异.为了更好的转换,请考虑 std::codecvt_utf8,如 http 中所述://en.cppreference.com/w/cpp/locale/codecvt_utf8

As pointed out in the comments and explained in https://stackoverflow.com/a/17106065/6345 there are cases when using the standard library to convert between UTF-8 and UTF-16 might give unexpected differences in the results on different platforms. For a better conversion, consider std::codecvt_utf8 as described on http://en.cppreference.com/w/cpp/locale/codecvt_utf8

注意(新):

由于 codecvt 标头在 C++17 中已弃用,因此引发了对此答案中提出的解决方案的一些担忧.但是,C++ 标准委员会在 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0618r0.html

Since the codecvt header is deprecated in C++17, some worry about the solution presented in this answer were raised. However, the C++ standards committee added an important statement in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0618r0.html saying

这个库组件应该与附件 D 一起退役,直到一个合适的替换被标准化.

this library component should be retired to Annex D, along side , until a suitable replacement is standardized.

因此在可预见的未来,这个答案中的 codecvt 解决方案是安全且可移植的.

So in the foreseeable future, the codecvt solution in this answer is safe and portable.

这篇关于C++ 将字符串(或 char*)转换为 wstring(或 wchar_t*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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