OS X 上的字符串到 wstring 转换

发布于 2024-08-18 06:06:39 字数 959 浏览 5 评论 0原文

我正在尝试将 C++ 字符串转换为 wstring。 我找到了以下代码,它似乎处理重音,这就是我正在寻找的。

std::wstring widen(const std::string& s)
{
    std::vector<wchar_t> buffer(s.size());
    std::locale loc("fr_FR");
    std::use_facet< std::ctype<wchar_t> >(loc).widen(s.data(), s.data() + s.size(), &buffer[0]);

    return std::wstring(&buffer[0], buffer.size());
}

来源

不幸的是,任何其他loc的代码都会崩溃 值高于 C 或 POSIX。这个问题已经在这里讨论过,但没有成功: MacOS 10.6 上的 std::locale 损坏(LANG=en_US.UTF-8),此处 或此处

有没有解决方法或其他方法来做到这一点?

I'm trying to convert a C++ string to a wstring.
I found the following code, that seems to deal with accents, which is what I'm looking for.

std::wstring widen(const std::string& s)
{
    std::vector<wchar_t> buffer(s.size());
    std::locale loc("fr_FR");
    std::use_facet< std::ctype<wchar_t> >(loc).widen(s.data(), s.data() + s.size(), &buffer[0]);

    return std::wstring(&buffer[0], buffer.size());
}

Source

Unfortunately, the code crashes for any other loc value than C or POSIX. This problem has already been discussed, without success, here: std::locale breakage on MacOS 10.6 with LANG=en_US.UTF-8, here or here.

Is there any workaround or an other way to do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-08-25 06:06:39

最简单的是

std::wstring w( s.begin(), s.end() );

......但要保留口音,您需要 codecvt 但是,这个示例可能有用。

The simplest would be

std::wstring w( s.begin(), s.end() );

... but to preserve accents you'd need codecvt however, this example might be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文