首先明确一点,这些都是C++定义的宏,为了让程序开发更方便快捷,下面是MSDN中的一个表格:
类型 | MBCS | Unicode |
WCHAR | wchar_t | wchar_t |
LPSTR | char* | char* |
LPCSTR | const char* | const char* |
LPWSTR | wchar_t* | wchar_t* |
LPCWSTR | const wchar_t* | const wchar_t* |
TCHAR | char | wchar_t |
LPTSTR | TCHAR*(或char*) | TCHAR* (或wchar_t*) |
LPCTSTR | const TCHAR* | const TCHAR* |
表中可以发现其实这么多宏,也就是两种基本的数据结构数据结构:char和wchar_t:
char是8位字符类型,最多只能包含256种字符,许多外文字符集所含的字符数目超过256个,char型无法表示。
wchar_t数据类型一般为16位或32位,但不同的C或C++库有不同的规定,如GNU Libc规定wchar_t为32位,总之,wchar_t所能表示的字符数远超char型。
参考博客:
http://m.blog.csdn.net/article/details?id=739717
可以用下列几种方法之一将它转换成char类型串:
1、调用 WideCharToMultiByte() API;
2、调用CRT 函数wcstombs(),注意,在转换英文的时候没有问题,在转换中文时,wcstombs()函数会返回-1;
3、使用CString 构造器或赋值操作(仅用于MFC );
4、使用ATL 串转换宏W2A()。
1.WideCharToMultiByte()
你可以用WideCharToMultiByte()将一个Unicode串转换成一个ANSI串。此函数的原型如下:
int WideCharToMultiByte ( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar );
以下是参数解释:
CodePage
Unicode字符转换成的代码页。你可以传递CP_ACP来使用当前的ANSI代码页。代码页是256个字符集。字符0――127与ANSI编码一样。字符128――255与ANSI字符不同,它可以包含图形字符或者读音符号。每一种语言或地区都有其自己的代码页,所以使用正确的代码页对于正确地显示重音字符很重要。
dwFlags
dwFlags 确定Windows如何处理“复合” Unicode字符,它是一种后面带读音符号的字符。如è就是一个复合字符。如果这些字符在CodePage参数指定的代码页中,不会出什么事。否则,Windows必须对之进行转换。
传递WC_COMPOSITECHECK使得这个API检查非映射复合字符。
传递WC_SEPCHARS使得Windows将字符分为两段,即字符加读音,如e`。
传递WC_DISCARDNS使得Windows丢弃读音符号。
传递WC_DEFAULTCHAR使得Windows用lpDefaultChar参数中说明的缺省字符替代复合字符。
缺省行为是WC_SEPCHARS。
lpWideCharStr
要转换的Unicode串。
cchWideChar
lpWideCharStr在Unicode 字符中的长度。通常传递-1,表示这个串是以0x00结尾。
lpMultiByteStr
接受转换的串的字符缓冲
cbMultiByte
lpMultiByteStr的字节大小。
lpDefaultChar
可选――当dwFlags包含WC_COMPOSITECHECK | WC_DEFAULTCHAR并且某个Unicode字符不能被映射到同等的ANSI串时所传递的一个单字符ANSI串,包含被插入的“缺省”字符。可以传递NULL,让API使用系统缺省字符(一种写法是一个问号)。
lpUsedDefaultChar
可选――指向BOOL类型的一个指针,设置它来表示是否缺省字符曾被插入ANSI串。可以传递NULL来忽略这个参数。
2,wcstombs()
这个CRT函数wcstombs()是个简化版,但它终结了WideCharToMultiByte()的调用,所以最终结果是一样的。其原型如下:
size_t wcstombs ( char* mbstr, const wchar_t* wcstr, size_t count );
以下是参数解释:
mbstr
接受结果ANSI串的字符(char)缓冲。
wcstr
要转换的Unicode串。
count
mbstr参数所指的缓冲大小。
wcstombs()在它对WideCharToMultiByte()的调用中使用WC_COMPOSITECHECK | WC_SEPCHARS标志。用wcstombs()转换前面例子中的Unicode串,结果一样:
wcstombs ( szANSIString, wszSomeString, sizeof(szANSIString) );
3.CString
MFC中的CString包含有构造函数和接受Unicode串的赋值操作,所以你可以用CString来实现转换。例如:
// 假设有一个Unicode串wszSomeString... CString str1 ( wszSomeString ); // 用构造器转换 CString str2; str2 = wszSomeString; // 用赋值操作转换
4.ATL宏
ATL有一组很方便的宏用于串的转换。W2A()用于将Unicode串转换为ANSI串(记忆方法是“wide to ANSI”――宽字符到ANSI)。
//使用ATL的W2A和A2W宏必须使用USES_CONVERSION USES_CONVERSION; //Unicode字符串 wchar_t* wszText=L"1.Unicode字符转换为ANSI;"; printf("%s\n",W2A(wszText)); //用wprintf输出非英文字符,需要设置当前的地域信息 setlocale(LC_ALL,"chs"); //ANSI字符串(ANSI:American National Standards Institute) //中文内码MBCS:Multi-Byte character sets,英文内码SBCS:Single-Byte character sets) char* szText="2.ANSI字符转换成Unicode."; wprintf(L"%s\n",A2W(szText));
如果要从char数据类型转换到wchar_t也有上述四种方法中三种方法的对应函数:
1、调用MultiByteToWideChar() API;
2、调用CRT 函数mbstowcs();
3、使用CString 构造器或赋值操作(仅用于MFC );
4、使用ATL 串转换宏A2W()。
最后附上两个String和WString相互转换的的函数:
//wstring转换成string std::string WChar2Ansi(LPCWSTR pwszSrc) { int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL); if (nLen <= 0) return std::string(""); char* pszDst = new char[nLen]; if (NULL == pszDst) return std::string(""); WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL); pszDst[nLen - 1] = 0; std::string strTemp(pszDst); delete[] pszDst; return strTemp; } //string转换车wstring std::wstring StringToWString(const std::string& s) { std::wstring wszStr; int nLength = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, NULL, NULL); wszStr.resize(nLength); LPWSTR lpwszStr = new wchar_t[nLength]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, lpwszStr, nLength); wszStr = lpwszStr; delete[] lpwszStr; return wszStr; }