wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString

来源:互联网 发布:卖家淘宝客如何推广 编辑:程序博客网 时间:2024/05/22 00:33
  1. #include <string>
  2. // 使用CString必须使用MFC,并且不可包含<windows.h>
  3. #define _AFXDLL
  4. #include <afx.h>
  5. using namespace std;
  6. //----------------------------------------------------------------------------------
  7. //将 单字节char* 转换为 宽字节 wchar*
  8. inline wchar_t* AnsiToUnicode( const char* szStr )
  9. {
  10. int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
  11. if (nLen == 0)
  12. {
  13.    return NULL;
  14. }
  15. wchar_t* pResult = new wchar_t[nLen];
  16. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
  17. return pResult;
  18. }
  19. //----------------------------------------------------------------------------------
  20. // 将 宽字节wchar_t* 转换 单字节char*
  21. inline char* UnicodeToAnsi( const wchar_t* szStr )
  22. {
  23. int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
  24. if (nLen == 0)
  25. {
  26.    return NULL;
  27. }
  28. char* pResult = new char[nLen];
  29. WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
  30. return pResult;
  31. }
  32. //----------------------------------------------------------------------------------
  33. // 将单字符 string 转换为宽字符 wstring
  34. inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr )
  35. {
  36. int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );
  37. wszStr.resize(nLength);
  38. LPWSTR lpwszStr = new wchar_t[nLength];
  39. MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );
  40. wszStr = lpwszStr;
  41. delete [] lpwszStr;
  42. }
  43. //----------------------------------------------------------------------------------
  44. int _tmain(int argc, _TCHAR* argv[])
  45. {
  46. char*   pChar = "我喜欢char";
  47. wchar_t* pWideChar = L"我讨厌wchar_t";
  48. wchar_t   tagWideCharList[100] ;
  49. char   ch = 'A';
  50. char   tagChar[100] = {NULL};
  51. CString   cStr;
  52. std::string str;
  53. // 注:设置语言环境以便输出WideChar
  54. setlocale(LC_ALL,"chs");
  55. // 注: char* 转换 wchar_t*
  56. // 注: wchar_t 未重载 << ,所以不可使用 cout << 输出
  57. pWideChar = AnsiToUnicode( pChar );
  58. // 注:printf("%ls") 和 wprintf(L"%s") 一致
  59. printf( "%ls/n", pWideChar ); 
  60. // 注:wchar_t* 转换 wchar_t[]
  61. wcscpy ( tagWideCharList, pWideChar );
  62. wprintf( L"%s/n", tagWideCharList );
  63. // 注:wchar_t[] 转换 wchar_t*
  64. pWideChar = tagWideCharList;
  65. wprintf( L"%s/n", pWideChar );
  66. // 注:char 转换 string
  67. str.insert( str.begin(), ch );
  68. cout << str << endl;
  69. // 注:wchar_t* 转换 string
  70. pWideChar = new wchar_t[str.length()];
  71. swprintf( pWideChar, L"%s", str.c_str());
  72. wprintf( L"%s/n", pWideChar );
  73. // 注:string 转换 char*
  74. pChar = const_cast<char*>(str.c_str());
  75. cout << pChar << endl;
  76. // 注:char* 转换 string
  77. str = std::string(pChar);
  78. // 注: cout 的 << 重载了string, 若printf 的话必须 printf("%s", str.c_str()); 
  79. //   而不可 print( "%s", str ); 因为 str 是个 string 类
  80. cout << str << endl;
  81. // 注:string 转换 char[]
  82. str = "无聊啊无聊";
  83. strcpy( tagChar, str.c_str() );
  84. printf( "%s/n", tagChar );
  85. // 注:string 转换 CString;
  86. cStr = str.c_str();
  87. // 注:CString 转换 string 
  88. str = string(cStr.GetBuffer(cStr.GetLength()));
  89. // 注:char* 转换 CString
  90. cStr = pChar;
  91. // 注:CString 转换 char*
  92. pChar = cStr.GetBuffer( cStr.GetLength() );
  93. // 注:CString 转换 char[]
  94. strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));
  95. // 注:CString 转换 wchar_t*
  96. pWideChar = cStr.AllocSysString();
  97. printf( "%ls/n", pWideChar );
  98. }