本文介绍了支持Unicode的可移植代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 让我们开始吧: 班级国家{ 公开: virtual const char * GetName()const = 0; } class挪威:public Nation { public: 虚拟const char * GetName()const { 返回"挪威" ;; } }; 假设我们想要用国家官方 语言给出这个国家的名字......所以我们想用Unicode字符集来实现这个目标。 如何在便携式代码中使用Unicode?类似 跟随?: typedef wchar_t UnicodeChar; class Nation { public: 虚拟const UnicodeChar * GetName()const = 0; } 类挪威:public Nation { public: virtual const UnicodeChar * GetName()const { return L" Norway" ;; //注意前面的L } }; 你会使用wchar_t,还是会使用unsigned short; ? (Unicode是16- 位)。 此外,你如何以这样的方式制作你的代码 使用普通字符或宽字符。微软做了以下事情 如下:(如果你使用的是Unicode,你定义UNICODE宏) #ifdef UNICODE typedef wchar_t字符; #define StringLiteral(x)Lx #else typedef char字符; #define StringLiteral(x)x #endif class Nation { public: 虚拟const字符* GetName()const = 0; } 类挪威:public Nation { public: 虚拟const字符* GetName()const { 返回StringLiteral(" Norway"); } }; 您怎么看待这个?目前我正在编写我想要的代码 支持普通字符集和Unicode ...但是我想保留它 便携式! 关于如何解决这个问题的任何建议?微软的方式是不够的? -TomásLet''s start off with:class Nation {public:virtual const char* GetName() const = 0;}class Norway : public Nation {public:virtual const char* GetName() const{return "Norway";}};Let''s say we want to give the name of the nation in the nation''s officiallanguage... and so we want to use the Unicode character set to achieve this.How does one go about using Unicode in portable code? Something like thefollowing?:typedef wchar_t UnicodeChar;class Nation {public:virtual const UnicodeChar* GetName() const = 0;}class Norway : public Nation {public:virtual const UnicodeChar* GetName() const{return L"Norway"; //Note the preceding L}};Would you use "wchar_t", or would you use "unsigned short"? (Unicode is 16-bit).Furthermore, how do you go about making your code in such a way that it canuse either normal characters or wide characters. Microsoft do it somethinglike the following: (You define the UNICODE macro if you''re using Unicode)#ifdef UNICODEtypedef wchar_t Character;#define StringLiteral(x) Lx#elsetypedef char Character;#define StringLiteral(x) x#endifclass Nation {public:virtual const Character* GetName() const = 0;}class Norway : public Nation {public:virtual const Character* GetName() const{return StringLiteral("Norway");}};What do you think of this? At the moment I''m writing code which I want tosupport the normal character set and also Unicode... but I want to keep itportable!Any suggestions on how to go about this? Is the Microsoft way decent enough?-Tomás推荐答案 我认为您需要确定它到底在做什么,并在Unicode上阅读 。 到目前为止,你只展示了广泛而狭隘的角色支持,并且 与编码无关。 您需要决定内部表示,然后您需要 为您选择的操作系统提供映射,可能通过流操作符 和facets。我不知道你对便携式产品的定义是什么。 Ben Pope - 我不是只是一个数字。对很多人来说,我被称为字符串...I think you need to decide what exactly it is you are doing, and read upon Unicode.So far you have only demonstrated wide and narrow character support, andnothing to do with encodings.You need to decide on an internal representation, and then you need toprovide mappings to your OS of choice, probably through stream operatorsand facets. I don''t know what your definition of portable is.Ben Pope--I''m not just a number. To many, I''m known as a string... Unicode定义为21位。 您可以使用各种编码来表示它,如UTF-8 ,UTF-16或 UTF-32别名UCS-4。 微软也使用UCS-2,但它不支持 整个Unicode范围。 如果您需要随机访问的东西,您只能使用UCS-2或UCS-4。 如果你只需要一个可逆容器,UTF-8或UTF-16就可以了。 无论如何你不应该使用指针作为字符串,而是字符串对象。 std :: wstring可以用于UCS-2或UCS-4,具体取决于你的系统。 要注意比标准中的更多,但是,std :: wstring wasn''为 unicode制作。你最好使用专门的IMO。 我不认为微软的UNICODE宏是一个好主意。这使得使用unicode支持编译的库不兼容 不等等。 只需让你的应用程序识别unicode,编译混乱的旗帜 一切都没用。 我建议使用glibmm的Glib :: ustring。 它包含关于一般Unicode内容的一些很好的工具。 还有来自IBM的ICU你可以查看。Unicode is defined on 21 bits.You can use various encodings to represent it, like UTF-8, UTF-16 orUTF-32 alias UCS-4.There is also UCS-2 that Microsoft uses, but it doesn''t support thewhole Unicode range.If you need something with Random Access, you can only take UCS-2 or UCS-4.If you only need a Reversible Container, UTF-8 or UTF-16 will do.Anyway you shouldn''t use pointers for strings, but strings objects.std::wstring can be used for UCS-2 or UCS-4 depending on your system.Be aware than in the standard, though, std::wstring wasn''t made forunicode. You''d better use something dedicated IMO.I don''t think the UNICODE macro of Microsoft is a good idea. That makeslibs compiled with unicode support incompatible with the ones whicharen''t etc.Just make your application unicode aware, compiling flags to messeverything up are useless.I would advise to use Glib::ustring from glibmm.It contains some nice tools about general Unicode stuff too.There is also ICU from IBM that you could check out. 什么是可逆的 ?如果UTF-16是可逆的,那么那么必须是UTF-32。 无论如何你不应该使用指针来表示字符串,而是使用字符串对象。 std :: wstring可以用于UCS-2或UCS-4,具体取决于你的系统。请注意,与标准相比,std :: wstring不是为了unicode而制作的。你最好使用专门的IMO。 我不认为微软的UNICODE宏是一个好主意。这使得使用unicode支持编译的库不兼容等等。只需让你的应用程序识别unicode,编译标记就会搞乱一切都没用。 我是第二个。 UTF-16也是浪费时间恕我直言。 我会建议使用来自glibmm的Glib :: ustring。它包含一些关于一般Unicode内容的好工具。 还有来自IBM的ICU,你可以查看。What is "Reversible" ? If UTF-16 is "reversible" then so must be UTF-32. Anyway you shouldn''t use pointers for strings, but strings objects. std::wstring can be used for UCS-2 or UCS-4 depending on your system. Be aware than in the standard, though, std::wstring wasn''t made for unicode. You''d better use something dedicated IMO. I don''t think the UNICODE macro of Microsoft is a good idea. That makes libs compiled with unicode support incompatible with the ones which aren''t etc. Just make your application unicode aware, compiling flags to mess everything up are useless.I second that.UTF-16 is also a big waste of time IMHO. I would advise to use Glib::ustring from glibmm. It contains some nice tools about general Unicode stuff too. There is also ICU from IBM that you could check out. 这篇关于支持Unicode的可移植代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 21:45