Windows核心编程学习笔记----第二章 UNICODE
摘自:http://www.cnblogs.com/txwd0033/archive/2009/07/19/1526671.html
1.运行库的Unicode支持
wchar_t * wcschr( const wchar_t * str, wchar_t c );
int strcmp( const char * string1, const char * string2 );
int wcscmp( const wchar_t * string1, const wchar_t * string2
);
char * strcpy( char * strDestination, const char * strSource
);
wchar_t * wcscpy(wchar_t * strDestination, const wchar_t * strSource );
size_t strlen( const char * str);
size_t wcslen( const wchar_t * str );
标准的ANSI C字符串函数以及对应的Unicode函数。
2.TChar.h
<Tchar.h>主要是为了帮助实现ANSI/Unicode通用源代码文件的编写。主要由一系列的宏组成,如果定义了_UNICODE宏,当进行源码编译时,宏引用wcs系列的函数,否则应用str系列的函数。在vs 2008带的头文件中,TCHAR的定义如下:
typedef WCHAR TCHAR, * PTCHAR;
VC++的编译器将所有的字符串默认为ANSI字串,而不是Unicode字符串。为了生成一个Unicode字符串,需要如下定义:
一个字符串前大写的L提示编译器,该字符串应编译为一个Unicode字符串。也可以使用Tchar.h重定义的宏_TEXT:
#define _TEXT(x) __T(x)
3.Windows下程序符合ANSI和Unicode的一些编程规范
- 将文本字符串看做字符数组,而不是字符型数据数组(??什么意思)或者字节数组;
- 对于文本字符和串使用通用数据类型(如TCHAR和PTSTR);
- 对字节、字节指针和数据缓冲区使用显式数据类型(如BYTE和PBYTE);
- 对文本字符和字符串使用TEXT宏;
- 执行全局替换(例如用PTSTR替换PSTR);
- 修改字符串运算问题。
4.Windows字符串函数
windows提供了一些支持Unicode的函数,这些函数和C运行库作用略有不同。具体的有lstrcat、lstrcmp、lstrcmpi、lstrcpy、lstrlen等
5.CompareString函数
int CompareString(LCID Locale, DWORD dwCmpFlags, LPCTSTR lpString1, int cchCount1, LPCTSTR lpString2, int cchCount2 );
各个参数的含义:
Locale,用来识别具体的语言种类。本函数使用LCID,通过检查使用于特定语言的字符的意义来比较两个字符串。
dwCmpFlags是比较标志,可以用来控制是否检查大小写等条件。
lpString1,lpString2分别是两个要比较的字符串。
cchCount1,cchCount2分别是他们的长度。如果为-1,则函数假定字符串以0结尾。
6.其他对于宽字符进行操作的windows函数
这些函数有CharLower、CharUpper、CharLowerBuff、CharUpperBuff、IsCharAlpha、IsCharAlphaNumberic、IsCharLower、IsCharUpper等,具体可以看msdn。
摘自:http://www.cnblogs.com/txwd0033/archive/2009/07/19/1526671.html
补充:
7.确定文本文件是包含ANSI字符还是Unicode字符。
DWORD IsTextUnicode(
CONST LPVOID lpBuffer, // pointer to an input buffer to be examined
int cb, // the size of the input buffer
LPINT lpi // flags that condition examination and receive results
);
8.在Unicode与ANSI之间转换字符串
The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.
int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // address of string to map
int cchMultiByte, // number of bytes in string
LPWSTR lpWideCharStr, // address of wide-character buffer
int cchWideChar // size of buffer
);
The WideCharToMultiByte function maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set.
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // address of wide-character string
int cchWideChar, // number of characters in string
LPSTR lpMultiByteStr, // address of buffer for new string
int cchMultiByte, // size of buffer
LPCSTR lpDefaultChar, // address of default for unmappable
// characters
LPBOOL lpUsedDefaultChar // address of flag set when default
// char. used
);