非主流帅哥图片大全:Windows核心编程(第五版)笔记 第二章 字符和字符串(Working with Ch...

来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 19:11:06
Windows核心编程(第五版)笔记 第二章 字符和字符串(Working with Characters and Strings)

 

第二章    字符和字符串(Working with Characters and Strings)

1.       字符的编码方式

UTF:Unicode Transformation Format

三种编码方式:UTF-8 UTF-16 UTF-32

UTF-8:根据字符的分类,分别用1~4字节来表示一个字符

UTF-32:所有的字符都用4字节表示

UTF-16:用两字节表示大部分语言的字符,当两节字无法表示时通过surrogates四字节表示字符

注:.Net FrameWork用UTF 16对所有字符进行编码,所以当需要在托管和非托管代码间传递字符或字符串时最好用UTF16统一字符的编码方式。这样可以提高程序的性能,节省内存

2.       Windows中自定义的字符类型

typedef char     CHAR;    // An 8-bit character

typedef unsigned short wchar_t;

typedef wchar_t  WCHAR;    // A 16-bit character

3.定义字符变量

wchar_t c = L'A';// A 16-bit character

wchar_t szBuffer[100] = L"A String";// An array up to 99 16-bit characters

and a 16-bit terminating zero.

     TCHAR c = TEXT('A');

 

L 告诉编译器它后面的字符要编译成Unicode string

TEXT宏用于编写ANSI和Unicode都能识别的代码。

其实现如下:

 

#ifdef UNICODE

    #define __TEXT(quote) L##quote

#else

    #define __TEXT(quote) quote

#endif

#define TEXT(quote)  __TEXT(quote)

4.头文件注释(Header Annotations)

头文件注释描述一个函数如何使用它的参数。在VS Enterprise版 中可以通过”Code Analysis”来分析函数的调用是否符合 Header annotation所定义的语义。

 

如:GetModuleFileName

(

    __in_opt HMODULE hModule,

    __out_ecount_part(nSize, return + 1) LPTSTR lpFilename,

    __in DWORD nSize

 );

_in The function reads from the buffer. The caller provides the buffer and initializes it.

_inout  The function both reads from and writes to buffer. The caller provides the buffer and initializes it. If used with _deref, the buffer may be reallocated by the function.

详见:http://msdn.microsoft.com/en-us/library/aa383701.aspx

5.Win API后缀 A/W

     为提供对ANSI和Unicode的支持。Win API分别针对ANSI和Unicode版本提供了不同的版本实现。

 

     如:  CreateWindowEx 有两个不同的实现

          CreateWindowExW  Unicode版

            CreateWindowExA   ANSI版

     我们在调用时不用关心具体去调用哪个版本,因为Windows已经处理好了。说见下:

 

     #ifdef UNICODE

#define CreateWindowEx     CreateWindowExW

#else

#define CreateWindowEx      CreateWindowExA

#endif

 

其实CreateWindowExA内部只是为把ANSI字符串转换成Unicode字符串 然后调用CreateWindowExW

6.C Run Time Library 与ANSI 和Unicode

     和Win API一样 C Run Time Library 提供一套库用于字符和字符串的操作。这个库中的每个函数也有两个版本,与Win API不同的是ANSI版本和Unicode版本的函数没有相互调用的关系, 而且针针ANSI和Unicode做了完全不同的实现。

   

    ASNI字符操作的头文件中String.h, 如果要写ANSI和Unicode都兼容的代码则还需引用文件它定义了一些宏,如下:

#ifdef _UNICODE

#define _tcslen     wcslen

#else

#define _tcslen     strlen

#endif

wcslen和strlen是CRT Library针对Unicode 和ANSI提供的求字符串长度的函数。

 

 

7.安全的字符串函数

    安全的字符串函数的头文件Strsafe.h

http://msdn2.microsoft.com/en-us/library/ms647466.aspx

cch 函数。如:StringCchCopy

    C安全函数列表

http://msdn2.microsoft.com/en-us/library/wd3wzwts(VS.80).aspx

    所有函数都以_S结尾,如strcat_s,  wcscat_s,  _mbscat_s

 

   HRESULT StringCchCopy      (PTSTR pszDest, size_t cchDest, PCTSTR pszSrc); 

   HRESULT StringCchCopyEx  (PTSTR pszDest, size_t cchDest, PCTSTR pszSrc, PTSTR *ppszDestEnd, size_t *pcchRemaining,DWORD dwFlags)

    HRESULT StringCbCopy        (LPTSTR pszDest,size_t cbDest,LPCTSTR pszSrc);

 

    其中Cch是Count of character的缩写,即字符的个数,一个字符可能占一字节,也可能占两字节,

    其值用_countof() 宏算出。

    WCHAR *w = L”abc”; 

    虽然sizeof(w) 是6 但 _countof(L”abc”) 等于3

 

    与Cch相反,Cb是Count of bye的缩写,即字节的个数

    StringCchCopy 即可用于ANSI也可用于UNICODE

  #ifdef UNICODE

             #define StringCchCopy StringCchCopyW

  #else

            #define StringCchCopy StringCchCopyA

  #endif // !UNICODE

 

    Ex代表用户可以控制字符串的操作。

 

    PTSTR 其中

          STR代表是字符串

           P   代表是指针

           T   代表该类型会根据UNICODE的宏对应相应的指针, char * 或 wchar_t *

 

 

    强制用UNICODE时, 应把UNICODE和_UNICODE宏定义在所有头文件的前面,如下:

#define UNICODE

#define _UNICODE

#include