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

时间:2022-06-24 22:40:44

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

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

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).

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

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

我应该期待什么并发症/头痛/惊喜?例如,如果我使用仍使用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.

由于对国际字符串的支持并不是对我所使用的嵌入式系统类型的强烈要求,我只会在不会引起严重问题的情况下这样做。

3 个解决方案

#1


1  

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.

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

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.

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

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

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

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.

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

#2


1  

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.

std :: wstring是在Windows上保存Unicode字符串的一个很好的选择,但在大多数其他平台上并不是这样,并且最终不适用于可移植代码。最好尝试坚持使用std :: string和UTF-8。

#3


1  

You might get some headache because of the fact that the C++ standard dictates that wide-streams are required to convert double-byte characters to single-byte when writing to a file, and how this conversion is done is implementation-dependent.

您可能会感到头疼,因为C ++标准规定在写入文件时需要使用宽流将双字节字符转换为单字节,以及如何完成此转换是依赖于实现的。

#1


1  

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.

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

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.

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

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

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

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.

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

#2


1  

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.

std :: wstring是在Windows上保存Unicode字符串的一个很好的选择,但在大多数其他平台上并不是这样,并且最终不适用于可移植代码。最好尝试坚持使用std :: string和UTF-8。

#3


1  

You might get some headache because of the fact that the C++ standard dictates that wide-streams are required to convert double-byte characters to single-byte when writing to a file, and how this conversion is done is implementation-dependent.

您可能会感到头疼,因为C ++标准规定在写入文件时需要使用宽流将双字节字符转换为单字节,以及如何完成此转换是依赖于实现的。