怎样用VC将字符串转换成unicode(20分)

L

lwaif

Unregistered / Unconfirmed
GUEST, unregistred user!
有没什么简单函数?
 
要用到编码表
 
那么麻烦的?去哪里找编码表啊?
 
_T("asdfasdfasd");
 
Maintaining a Single Source
There are, of course, certain disadvantages to using Unicode. First and foremost is that every string in your program will occupy twice as much space. In addition, you'll observe that the functions in the wide-character run-time library are larger than the usual functions. For this reason, you might want to create two versions of your program—one with ASCII strings and the other with Unicode strings. The best solution would be to maintain a single source code file that you could compile for either ASCII or Unicode.
That's a bit of a problem, though, because the run-time library functions have different names, you're defining characters differently, and then
there's that nuisance of preceding the string literals with an L.
One answer is to use the TCHAR.H header file included with Microsoft Visual C++. This header file is not part of the ANSI C standard, so every function and macro definition defined therein is preceded by an underscore. TCHAR.H provides a set of alternative names for the normal run-time library functions requiring string parameters (for example, _tprintf and _tcslen). These are sometimes referred to as "generic" function names because they can refer to either the Unicode or non-Unicode versions of the functions.
If an identifier named _UNICODE is defined and the TCHAR.H header file is included in your program, _tcslen is defined to be wcslen:

#define _tcslen wcslen
If UNICODE isn't defined, _tcslen is defined to be strlen:

#define _tcslen strlen
And so on. TCHAR.H also solves the problem of the two character data types with a new data type named TCHAR. If the _UNICODE identifier is defined, TCHAR is wchar_t:

typedef wchar_t TCHAR
Otherwise, TCHAR is simply a char:

typedef char TCHAR
Now it's time to address that sticky L problem with the string literals. If the _UNICODE identifier is defined, a macro called __T is defined like this:

#define __T(x) L##x
This is fairly obscure syntax, but it's in the ANSI C standard for the C preprocessor. That pair of number signs is called a "token paste," and it causes the letter L to be appended to the macro parameter. Thus, if the macro parameter is "Hello!", then
L##x is L"Hello!".
If the _UNICODE identifier is not defined, the __T macro is simply defined in the following way:

#define __T(x) x
Regardless, two other macros are defined to be the same as __T:

#define _T(x) __T(x)
#define _TEXT(x) __T(x)
Which one you use for your Win32 console programs depends on how concise or verbose you'd like to be. Basically, you must define your string literals inside the _T or _TEXT macro in the following way:

_TEXT ("Hello!")
Doing so causes the string to be interpreted as composed of wide characters if the _UNICODE identifier is defined and as 8-bit characters if not.
总而言之 使用 _T TCHAR 就可以
 

Similar threads

回复
0
查看
701
不得闲
回复
0
查看
886
不得闲
回复
0
查看
708
不得闲
S
回复
0
查看
977
swish
S
顶部