VC++ 中 Unicode 字符串的语法是什么?
在 VC++ 中应该如何使用 unicode 字符串?当然你应该#define UNICODE,但是你的字符串呢?
应该在所有文本周围使用 TEXT() 或 _T() 宏,还是应该在字符串前面放置一个 L?我相信现在所有的程序都应该使用 unicode,所以使用 L 前缀不是最干净的吗?
意见?
How should you use unicode strings in VC++? Of course you should to #define UNICODE, but what about your strings?
Should the TEXT() or _T() macro be used around all text or should you just put an L in front of strings? Its my belief that all programs should use unicode these days, so wouldn't it be cleanest to use use the L prefix?
Opinions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您想要实现的目标。如果您想确保代码在使用或不使用 Unicode 的情况下都能正确编译和工作,请使用
TEXT
或_T
宏,并调用“默认”Win32 函数名称 (例如CreateWindow
)。如果您想确保您的程序始终使用 Unicode API,那么您应该在字符串前面使用
L
前缀,并调用 Win32 函数的宽版本(例如CreateWindowW< /代码>)。
在后一种情况下,无论是否定义了 UNICODE,您都将获得 unicode 行为。
在前一种情况下,您的应用程序将根据是否定义了 UNICODE 来更改其行为。
我同意你的观点,自 Win98 以来,非 unicode 版本并没有真正相关,所以我会采用第二种方法。
It depends on what you want to achieve. If you want to make sure your code will compile and work correctly both with and without Unicode, use the
TEXT
or_T
macros, and call the "default" Win32 function names (for exampleCreateWindow
).If you want to make sure your program always uses the Unicode API, then you should use a
L
prefix in front of your strings, and call the wide versions of Win32 functions (such asCreateWindowW
).In the latter case, you'll get unicode behavior whether or not
UNICODE
is defined.In the former case, your application will change its behavior based on whether
UNICODE
is defined.I agree with you that the non-unicode versions haven't really been relevant since Win98, so I'd go with the second approach.
使用
L
前缀声明 Unicode 字符串文字。TEXT()
或_T()
宏适用于过去的糟糕日子,当时您希望为 Unicode 和非 Unicode 版本的 Windows (Windows 9x) 编译单一源代码。值得庆幸的是,您现在可以安全地忽略 Windows 9x。Declare Unicode string literals with
L
prefix.The
TEXT()
or_T()
macros were for the bad old days when you wanted single source to compile for both Unicode and non-Unicode versions of Windows (Windows 9x). Thankfully you can safely ignore Windows 9x today.我不久前学到的东西:
黄金法则:
不要对抗框架。
按照框架的设计目的去做——如果你使用 Windows,请使用
_T
,使您的代码独立于字符类型。如果您使用的是 Linux,请使用 UTF-8。如果你有一个跨平台框架,就做它能做的事情。但不要尝试发明自己的东西,除非你有真正的充分理由。 (通常不值得在框架上花费精力。)Something I learned a while ago:
Golden rule:
Don't fight the framework.
Do what the framework was designed to do -- if you use Windows, use
_T
, to make your code independent of the character type. If you're on Linux, use UTF-8. If you have a cross-platform framework, do whatever it does. But don't try to invent something of your own unless you have a really good reason to. (It is simply usually not worth the effort of working against a framework.)