简体   繁体   English

从 std::wstring 转换为 std::string

[英]Convert from std::wstring to std::string

I'm converting wstring to string with std::codecvt_utf8 as described in this question , but when I tried Greek or Chinese alphabet symbols are corrupted, I can see it in the debug Locals window, for example 日本 became "日本"我正在将 wstring 转换为带有 std::codecvt_utf8 的字符串,如this question中所述,但是当我尝试希腊或中文字母符号已损坏时,我可以在调试 Locals window 中看到它,例如日本变成“日斬 "

std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv; //also tried codecvt_utf8_utf16
std::string str = myconv.to_bytes(wstr);

What am I doing wrong?我究竟做错了什么?

std::string simply holds an array of bytes. std::string只保存一个字节数组。 It does not hold information about the encoding in which these bytes are supposed to be interpreted, nor do the standard library functions or std::string member functions generally assume anything about the encoding.它不包含有关应该解释这些字节的编码的信息,标准库函数或std::string成员函数通常也不假设有关编码的任何信息。 They handle the contents as just an array of bytes.它们将内容作为字节数组处理。

Therefore when the contents of a std::string need to be presented, the presenter needs to make some guess about the intended encoding of the string, if that information is not provided in some other way.因此,当需要显示std::string的内容时,如果没有以其他方式提供该信息,则演示者需要对字符串的预期编码进行一些猜测。

I am assuming that the encoding you intend to convert to is UTF8, given that you are using std::codecvt_utf8 .假设您使用的是std::codecvt_utf8 ,我假设您打算转换为的编码是 UTF8 。

But if you are using Virtual Studio, the debugger simply assumes one specific encoding, at least by default.但是,如果您使用的是 Virtual Studio,调试器只会假定一种特定的编码,至少在默认情况下是这样。 That encoding is not UTF8, but I suppose probably code page 1252.该编码不是UTF8,但我想可能是代码页1252。

As verification, python gives the following:作为验证,python 给出以下信息:

>>> '日本'.encode('utf8').decode('cp1252')
'日本'

Your string does seem to be the UTF8 encoding of日本interpreted as if it was cp1252 encoded.您的字符串似乎是日本的 UTF8 编码,被解释为好像是 cp1252 编码的。

Therefore the conversion seems to have worked as intended.因此,转换似乎已按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM