比较来自世界各地的卖家的域名和 IT 服务价格

存储字符串 unicode UTF-8 在 std::string

回答讨论

https://coderoad.ru/4169948/
https://coderoad.ru/2722951/
我正在尝试分配一个字符串
UTF-8

多变的
std::string

在环境中
Visual Studio 2010



std::string msg = "महसुस";


但是,当我查看一个字符串提交调试器时,我只看到了 "?????"
我有一个文件保存为 Unicode /UTF-8 带签名/
, 我使用一组角色 "use unicode character set"

"महसुस"- 这是尼泊尔,它包含 5 符号和占用 15 字节。 但是调试器 visual studio 显示尺寸 msg 作为 5

我的问题是:

如何使用 std::string, 只是商店 utf-8 不必操纵它们

?
已邀请:

奔跑吧少年

赞同来自:

如果你用过 C++11, 这很简单:


std::string msg = u8"महसुस";


但是因为这不是这样的,你可以使用转义序列,而不是依靠编码源文件来控制你的编码,所以你的代码更容易可容忍 /如果您不小心以除此之外的格式保存 UTF8/:


std::string msg = "\xE0\xA4\xAE\xE0\xA4\xB9\xE0\xA4\xB8\xE0\xA5\x81\xE0\xA4\xB8"; // "महसुस"


否则,您可以考虑在执行期间转换的可能性:


std::string toUtf8/const std::wstring &str/
{
std::string ret;
int len = WideCharToMultiByte/CP_UTF8, 0, str.c_str//, str.length//, NULL, 0, NULL, NULL/;
if /len > 0/
{
ret.resize/len/;
WideCharToMultiByte/CP_UTF8, 0, str.c_str//, str.length//, &ret[0], len, NULL, NULL/;
}
return ret;
}



std::string msg = toUtf8/L"महसुस"/;

喜特乐

赞同来自:

你可以写
msg.c_str//, s8

在窗口窗口中正确看字符串 UTF-8.

窦买办

赞同来自:

如果你有 C++11, 你可以写
u8"महसुस"

. 否则,您必须使用使用的实际字节序列
\x
<i>
xx
</i>

对于序列中的每个字节 UTF-8.

通常,您最好从配置文件中读取此类文本。

龙天

赞同来自:

如果设置系统 locale 进入英文,文件是 UTF-8 没有 BOM, VC 允许您保存字符串 as-is.
https://raymai97.github.io/myb ... .html
https://i.stack.imgur.com/grZvL.png

快网

赞同来自:

有一种方法可以显示正确的值谢谢
http://msdn.microsoft.com/en-u ... .aspx
's8'. 如果我们补充说 ', s8' 变量名称, Visual Studio 远程文本B. UTF-8 文本将正确显示:

如果您正在使用 Microsoft Visual Studio 2008 Service Pack 1, 你需要纠正修正

http://support.microsoft.com/kb/980263

要回复问题请先登录注册