如何以跨平台友好的方式处理C / C ++中的Unicode字符串? [英] How to deal with Unicode strings in C/C++ in a cross-platform friendly way?

查看:102
本文介绍了如何以跨平台友好的方式处理C / C ++中的Unicode字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不同于Windows的平台上,您可以轻松地使用 char * 字符串并将其视为UTF-8。



问题在于,在Windows上,您需要使用wchar *字符串(W)接受和发送消息。如果您将使用ANSI函数(A),则将不支持Unicode。



因此,如果您要编写真正可移植的应用程序,则需要在Windows上将其编译为Unicode。



现在,为了保持代码的整洁,我想看看推荐的处理字符串的方法是什么,该方法可以最大程度地减少代码中的难看程度。 / p>

您可能需要的字符串类型: std :: string std :: wstring std :: tstring char * wchat_t * TCHAR * CString (ATL 1)。



问题您可能会遇到:




  • cout / cerr / cin 及其Unicode变体 wcout,wcerr,wcin

  • 所有重命名的宽字符串函数及其TCHAR宏-如 strcmp wcscmp _tcscmp

  • 代码中的常量字符串,使用TCHAR,您将不得不用 _T()宏填充代码。


您认为哪种方法最好?(欢迎示例)



我个人会采用 std :: tstring 的方法,但是我想看看在必要的情况下如何进行转换。

解决方案

我只能建议您检出此库: http://cppcms.sourceforge.net/boost_locale/docs/

可能会有所帮助,目前它是一个不错的选择,但我相信它将成功。


On platforms different than Windows you could easily use char * strings and treat them as UTF-8.

The problem is that on Windows you are required to accept and send messages using wchar* strings (W). If you'll use the ANSI functions (A) you will not support Unicode.

So if you want to write truly portable application you need to compile it as Unicode on Windows.

Now, In order to keep the code clean I would like to see what is the recommended way of dealing with strings, a way that minimize ugliness in the code.

Type of strings you may need: std::string, std::wstring, std::tstring,char *,wchat_t *, TCHAR*, CString (ATL one).

Issues you may encounter:

  • cout/cerr/cin and their Unicode variants wcout,wcerr,wcin
  • all renamed wide string functions and their TCHAR macros - like strcmp, wcscmp and _tcscmp.
  • constant strings inside code, with TCHAR you will have to fill your code with _T() macros.

What approach do you see as being best? (examples are welcome)

Personally I would go for a std::tstring approach but I would like to see how would do to the conversions where they are necessary.

解决方案

I can only suggest you to check this library out: http://cppcms.sourceforge.net/boost_locale/docs/
It might help, it's a boost candidate for now but I believe it will make it.

这篇关于如何以跨平台友好的方式处理C / C ++中的Unicode字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆