Microsoft 使用什么作为 Unicode 字符串的数据类型? [英] What is Microsoft using as the data type for Unicode Strings?

查看:36
本文介绍了Microsoft 使用什么作为 Unicode 字符串的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C++,在 MSDN 上看到了一篇文章:

I am in the process of learning C++ and came across an article on the MSDN here:

http://msdn.microsoft.com/en-us/magazine/dd861344.aspx

在第一个代码示例中,与我的问题相关的一行代码如下:

In the first code example the one line of code which my question relates to is the following:

VERIFY(SetWindowText(L"Direct2D Sample"));

更具体地说是 L 前缀.我读了一点,如果我错了,请纠正我:-),但这是为了允许 unicode 字符串,即为长字符集做准备.现在,在我阅读这篇文章的过程中,我发现了另一篇关于 C 语言高级字符串技术的文章 http://www.flipcode.com/archives/Advanced_String_Techniques_in_C-Part_I_Unicode.shtml

More specifically that L prefix. I had a little read up, and correct me if I am wrong :-), but this is to allow for unicode strings, i.e. to prep for a long character set. Now in during my read up on this I came across another article on Adavnced String Techniques in C here http://www.flipcode.com/archives/Advanced_String_Techniques_in_C-Part_I_Unicode.shtml

它说有几个选项,包括包含标题:

It says there are a few options including the inclusion of the header:

#define UNICODE 

#define _UNICODE

在 C 中,如果我错了,再次指出,感谢您的反馈.此外,它显示适用于这些 un​​icode 字符串的数据类型是:

in C , again point out if I am wrong, appreciate your feedback. Further it shows the datatype suitable for these unicode strings being:

wchar_t

它混合了一个宏和一种混合数据类型,宏是:

It throws into the mix a macro and a kind of hybrid datatype, the macro being:

_TEXT(t)

它只是在字符串前面加上 L 和混合数据类型

which simply prefixes the string with the L and the hybrid data type as

TCHAR 

如果标头在那里,它指出将允许使用 unicode,否则将允许使用 ASCII.现在我的问题是,或者更多的是我想确认的假设,Microsoft 是否会使用这种更灵活的 TCHAR 数据类型,或者承诺使用 wchar_t 是否有任何好处.

Which it points out will allow for unicode if the header is there and ASCII if not. Now my question is, or more of an asumption which I would like to confirm, would Microsoft use this TCHAR data type which is more flexible or is there any benefit to committing to using the wchar_t.

另外,当我说微软是否使用它时,更具体地说是在 ATL 和 WTL 库中的例子,你们中的任何人对此有偏好或有一些建议吗?

Also when I say does Microsoft use this, more specifically for exmaple in the ATL and WTL libraries, do anyone of yourselves have preference or have some advice regarding this?

干杯,

安德鲁

推荐答案

对于所有新软件,您应该定义 UNICODE 并直接使用 wchar_t.使用 ANSI 搅拌器会再次困扰您.

For all new software you should define UNICODE and use wchar_t directly. Using ANSI stirngs will come back to haunt you.

您应该只使用 wchar_t 和所有 CRT 函数的宽版本(例如:wcscmp 而不是 strcmp).如果您的代码需要在 ANSI 和 UNICODE 环境中工作,而我认为代码很少需要这样做,那么 TEXT 宏和 TCHAR 等就会存在.

You should just use wchar_t and the wide versions of all the CRT functions (ex: wcscmp instead of strcmp). The TEXT macros and TCHAR etc just exist if your code needs to work in both ANSI and UNICODE environments which I feel code rarely needs to do.

当您使用 Visual Studio 创建新的 Windows 应用程序时,会自动定义 UNICODE,并且 wchar_t 将像内置程序一样工作.

When you create a new windows application using Visual Studio UNICODE is automatically defined and wchar_t will work like a built-in.

这篇关于Microsoft 使用什么作为 Unicode 字符串的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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