C++中的字符(串)指针

(一)char       wchar_t(WCHAR)       TCHAR
ANSI:char是8位ANSI字符的数据类型,可用字符串处理函数:strcat( ),strcpy( ), strlen( )等以str打头的函数。
UNICODE:wchar_t是16位Unicode字符的数据类型,可用字符串处理函数:wcscat(),wcscpy(),wcslen()等以wcs打头的函数。它实际定义在里:typedef unsigned short wchar_t。在头文件中有这样的定义:typedef wchar_t WCHAR; 所以WCHAR实际就是wchar_t。为了让编译器识别Unicode字符串,必须以在前面加一个“L”,例如: wchar_t *szTest=L"This is a Unicode string."。
VC++:VC++中的TCHAR在ANSI和UNICODE环境中分别代表char和wchar_t(WCHAR)。如下定义:
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;;
#endif
(二)LPSTR       LPCSTR      LPWSTR     LPCWSTR       LPTSTR         LPCTSTR
ANSI:LPSTR、LPCSTR是ANSI字符的字符串指针类型,分别是一个普通字符串、一个常量字符串,分别代表“long pointer to a generic string”,“long pointer to a constant generic string“。都是一个指向以NULL(‘’)结尾的8位ANSI字符数组指针
UNICODE:LPWSTR、LPCWSTR是UNICODE字符的字符串指针类型,分别是一个普通双字节字符串、一个常量双字节字符串,分别代表“long pointer to a generic dword string”,“long pointer to a constant generic dword  string“。都是一个指向以NULL(‘’)结尾的16位UNICODE字符数组指针。
在使用字符串常量的时候需要使用_TEXT(“MyStr”)或者_T("")来支持系统的自动转换。
VC++:VC++中的LPTSTR、LPCTSTR在ANSI和UNICODE环境中分别代表LPSTR、LPCSTR和LPWSTR、LPCWSTR。如下定义:
#ifdef UNICODE
typedef LPWSTR LPTSTR;
typedef LPCWSTR LPCTSTR;
#else
typedef LPSTR LPTSTR;
typedef LPCSTR LPCTSTR;
#endif
(三)相互转换

 (1) 同字符集下char*转换成CString

  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:

char chArray[] = "This is a test";
char * p = "This is a test";

  或

LPSTR p = "This is a test";

  或在已定义Unicode应的用程序中

TCHAR * p = _T("This is a test");

  或

LPTSTR p = _T("This is a test");
CString theString = chArray;
theString.Format(_T("%s"), chArray);
theString = p;

  (2) 同字符集下CString转换成char*

  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:

  方法一,使用强制转换。例如:

CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

  方法二,使用strcpy。例如:

CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);

  需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

  方法三,使用CString::GetBuffer。例如:

CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('');
s.ReleaseBuffer();
// 使用完后及时释放,以便能使用其它的CString成员函数

(3) ANSI、Unicode和宽字符之间的转换

  方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。

  方法二,使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String*对象。例如:

TCHAR tstr[] = _T("this is a test");
wchar_t wszStr[] = L"This is a test";
String* str = S”This is a test”;

  方法三,使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,强烈推荐使用ALT宏进行转换,很方便:

  例如,CA2CT就是将ANSI转换成一般类型的字符串常量。其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”。SourceType和DestinationType可以是A、W、T和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。。为了使用这些宏,必须在函数的开始处用USES_CONVERSION来初始化某些局部变量.下面是一些示例代码:

USES_CONVERSION;

W2A(CString);把UNICODE的CString转换成char *返回。

A2W(char *);把char *转换成unicode的WCHAR返回。

T2A,A2T在unicode环境下T表示W,在ansi环境下T表示A。

LPTSTR tstr= CA2TEX<16>("this is a test");
LPCTSTR tcstr= CA2CT("this is a test");
wchar_t wszStr[] = L"This is a test";
char* chstr = CW2A(wszStr);

原文地址:https://www.cnblogs.com/swek/p/4338063.html