对于嵌入式应用程序,从 std::string 切换到 std::wstring ?

发布于 2024-07-06 18:29:47 字数 303 浏览 4 评论 0原文

到目前为止,我一直在嵌入式系统(路由器、交换机、电信设备等)的 C++ 应用程序中使用 std::string。

对于下一个项目,我正在考虑从 std::string 切换到 std::wstring 以支持 Unicode。 例如,这将允许最终用户在命令行界面 (CLI) 中使用中文字符。

我应该期待哪些并发症/头痛/意外? 例如,如果我使用仍然使用 std::string 的第三方库怎么办?

由于对国际字符串的支持对于我所从事的嵌入式系统类型来说并不是那么强烈的要求,因此只有在不会引起重大麻烦的情况下我才会这样做。

Up until now I have been using std::string in my C++ applications for embedded system (routers, switches, telco gear, etc.).

For the next project, I am considering to switch from std::string to std::wstring for Unicode support. This would, for example, allow end-users to use Chinese characters in the command line interface (CLI).

What complications / headaches / surprises should I expect? What, for example, if I use a third-party library which still uses std::string?

Since support for international strings isn't that strong of a requirement for the type of embedded systems that I work on, I would only do it if it isn't going to cause major headaches.

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

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

发布评论

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

评论(3

江心雾 2024-07-13 18:29:47

std::wstring 是在 Windows 上保存 Unicode 字符串的不错选择,但在大多数其他平台上则不然,当然也不适合可移植代码。 最好尝试坚持使用 std::string 和 UTF-8。

std::wstring is a good choice for holding Unicode strings on Windows, but not on most other platforms, and ceirtanly not for a portable code. Better try to stick with std::string and UTF-8.

中性美 2024-07-13 18:29:47

请注意,许多通信协议需要 8 位字符(或 7 位字符或其他类型),因此您经常需要在内部 wchar_t/wstring 数据和外部编码之间进行转换。

当您需要 Unicode 字符的 8 位表示时,UTF-8 编码非常有用。 (请参阅如何编写安全的代码UTF-8? 了解更多信息。)但请注意,您可能需要支持其他编码。

越来越多的第三方库支持 Unicode,但仍有很多不支持。

我真的无法告诉你这是否值得让你头疼。 这取决于您的要求。 如果您从头开始,那么从 std::wstring 开始比稍后从 std::string 转换为 std::wstring 更容易。

Note that many communications protocols require 8-bit characters (or 7-bit characters, or other varieties), so you will often need to translate between your internal wchar_t/wstring data and external encodings.

UTF-8 encoding is useful when you need to have an 8-bit representation of Unicode characters. (See How Do You Write Code That Is Safe for UTF-8? for some more info.) But note that you may need to support other encodings.

More and more third-party libraries are supporting Unicode, but there are still plenty that don't.

I can't really tell you whether it is worth the headaches. It depends on what your requirements are. If you are starting from scratch, then it will be easier to start with std::wstring than converting from std::string to std::wstring later.

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