1 #include <string> 2 #include <tchar.h> // _TCHAR 3 #include <stdlib.h> 4 #include <iostream> 5 #include <atlstr.h> //Cstring 6 #include <atlconv.h> //W2A宏 7 #include <xstring> 8 9 using namespace std; 10 11 int _tmain(int argc,_TCHAR* argv[]) 12 { 13 /**********string->cstring ***********/ 14 //setlocale(LC_ALL, "Chinese-simplified"); 15 //wcout.imbue(locale("chs")); 16 //std::string str = "123456!"; 17 //CString cstr = _T(""); 18 //方式一 19 //cstr.Format(_T("%s"),str.c_str()); 20 //方式二 21 //for(int i = 0; i < (int)str.length(); ++i) 22 //{ 23 // cstr += str[i]; 24 //} 25 //方式三 26 //cstr = str.c_str(); 27 //wcout << cstr << endl; 28 29 /************ cstring->string **************/ 30 //方式一 31 //CString cstr1 = _T("this is cstring!"); 32 //string str1 = ""; 33 //str1 = cstr1.GetBuffer(); 34 //cstr1.ReleaseBuffer(); 35 //方式二 36 //for (int i = 0; i < cstr1.GetLength(); ++i) 37 //{ 38 // str1 += cstr1[i]; 39 //} 40 //方式三 W2A宏 41 //str1 = W2A(cstr1);//用法还没弄懂,需要加入一些文件头 或宏定义 ??? 42 43 //cout << str1 << endl; 44 45 /************* string-> char*************/ 46 //string str2 = "this is string2!"; 47 //方式一 48 //char *p = (LPSTR)str2.c_str(); 49 //方式二 50 //char t[200]; 51 //memset(t, 0,200); 52 //strcpy(t,str2.c_str()); 53 //方式三 出现warning C4305: “=”: 从“int”到“char”截断 ??? 54 //char *pc = new char[str2.length() + 1]; 55 //pc[str2.length()] = '/0'; 56 //for (int i = 0; i < str2.length(); ++i) 57 //{ 58 // pc[i] = str2[i]; 59 //} 60 //pc[str2.length()] = '/0'; 61 //cout << pc << endl; 62 63 /****************** char*->string ******************/ 64 //char *pch = "this a char!"; 65 //方式一 只能用作初始化 66 //string str3(pch); 67 //方式二 assign()赋值 68 //string str3; 69 //str3.assign(pch); 70 //cout << str3 << endl; 71 72 //string是ansi编码字符char 73 //TCHAR是unicode编码字符wchar_t 74 /*********** string -> TCHAR* *********/ 75 //使用MultiByteToWideChar()函数 76 77 /*********** TCHAR* ->string *********/ 78 //使用 WideCharToMultiByte()函数 79 80 /**************** string 和char*转int ******************/ 81 //int atoi(const char *nptr); 82 //long atol(const char *nptr); 83 //long long atoll(const char *nptr); 84 //long long atoq(const char *nptr); 85 86 /*******************int转char*和string************************/ 87 //使用itoa();itoa(i,str,10); 88 89 /******************* wstring转Csting *****************/ 90 //wstring wstr = L"this a wstring!你好啊"; 91 //CString cstr4 = _T(""); 92 //cstr4 = wstr.c_str(); 93 //cout << cstr4 << endl; 94 95 /******************* Csting ->wstring *****************/ 96 //CString cstr5 = _T("this a cstring5!"); 97 //wstring wstr5 = L""; 98 //wstr5 = cstr5.AllocSysString(); 99 //wcout << wstr5 << endl; 100 101 /********* Cstring转char* *************/ 102 //CString cstr6 = _T("this cstring6!"); 103 //char* pch6 = ""; 104 //pch6 = cstr6.GetBuffer(); 105 //cout << pch6 << endl; 106 //cstr6.ReleaseBuffer(); 107 108 /************ char *转Cstring **********/ 109 //char pch[20] = "this a char* !"; 110 //CString str7 = _T(""); 111 //str7.Format("%s",pch); 112 //cout << str7 << endl; 113 114 /*********** TCHAR -> char ************/ 115 //TCHAR *pTch = _T("this a TCHAR"); 116 //int nLength = 2 * strlen(pTch); 117 //char* pCh = new char[nLength + 1]; 118 //memset(pCh,0, nLength + 1); 119 //strcpy(pCh, pTch); 120 //cout << pCh << endl; 121 122 /*********** char转tchar ************/ 123 124 system("pause"); 125 return 0; 126 }