string, CStringA, char*与wstring, CStringW, wchar_t*相互转换
1. char*转换为wchar_t*
char buf[] = "我是韩长鸣haizeiwanghancm";
wchar_t wbuf[100];
1.1. C的方式:最可移植的方式
1.1.1. mbstowcs
setlocale(LC_CTYPE, "");
mbstowcs(wbuf, buf, sizeof(buf));
1.1.2. swprintf
setlocale(LC_CTYPE, "");
swprintf(wbuf, L"%S", buf);
1.2. Windows平台的方式
1.2.1. wsprintf
wsprintf(wbuf, L"%S", buf);
1.2.2. MultiByteToWideChar
//返回需要的多字节数目(包括结尾'\0')
int len = MultiByteToWideChar(
CP_ACP, //多字节相关的代码页
0, //额外控制,影响变音符号,一般设为0
buf, //char缓冲区
-1, //char字符个数,-1函数自动判断长度
NULL, //wchar_t缓冲区
0); //wchar_t缓冲区最大字节数,传入0:不执行转换,返回需要的宽字节数目(包括结尾'\0')
wchar_t *wbuf2 = new wchar_t[len * sizeof(wchar_t)];
if (wbuf == nullptr) {
return 0;
}
MultiByteToWideChar(CP_ACP, 0, buf, -1, wbuf2, len);
1.3. ATL,MFC可用的方式
1.3.1. A2W
USES_CONVERSION;
MessageBoxW(nullptr, A2W(buf), nullptr, 0);
1.3.2. CStringW
CStringW str = buf;
//这里存在一个operator const wchar_t*的操作符重载
MessageBox(nullptr, str, nullptr, 0);
2. wchar_t*转char*相似的过程,看最后的程序
3. 附赠: CStringW, CStringA, wstring,string, wchar_t*, char*的相互转换
//7. string转wchar_t*
string str1 = "string方式, jsfoej韩长鸣fiok";
wstring ch(str1.size(), 0);
setlocale(LC_CTYPE, "");
mbstowcs(&ch[0], str1.c_str(), str1.size());
MessageBoxW(nullptr, ch.c_str(), nullptr, 0);
//8. CStringA转wchar_t*
CStringA CStr = "CStringA方式jsofj韩长鸣eof";
CStringW WStr = CStr;
MessageBox(nullptr, WStr, nullptr, 0);
//9. CStringA转string,直接赋值
CStringA Astr(L"韩长鸣sjfo");
string s = Astr;
cout << s << endl;
//10. string转CStringA, CString = string.c_str();
//11. CStringA转wstring: CStringW = CStringA; wstring = CStringW;
//12. string转CStringW: CStringA = string.cstr(); CStringW = CStringA;
//7. wstring转char*
wstring str1 = L"wstring方式, jsfoej韩长鸣fiok";
string ch(str1.size(), 0);
setlocale(LC_CTYPE, "");
wcstombs(&ch[0], str1.c_str(), str1.size());
MessageBoxA(nullptr, ch.c_str(), nullptr, 0);
//8. CStringW转char*
CStringW CStr = "CStringW方式jsofj韩长鸣eof";
CStringA AStr = CStr;
MessageBoxA(nullptr, AStr, nullptr, 0);
//9. CStringW转wstring,直接赋值
CStringW wstr(L"韩长鸣sjfo");
wstring ws = wstr;
wcout << ws << endl;
//10. wstring转CStringW, CStringW = wstring.c_str();
//11. CStringW转string: CStringA = CStringW; string = CStringA;
//12. wstring转CStringA: CStringW = wstring.cstr(); CStringA = CStringW;
//char*转wchar_t*
1 #include <Windows.h> //for wsprintf, MultiByteToWideChar,WideCharToMultiByte 2 #include <cstdio> //for swprintf 3 #include <atlconv.h> //for A2W(),W2A() 4 #include <atlstr.h> //for CString 5 #include <locale> 6 #include <string> 7 #include <iostream> 8 9 using std::cout; 10 using std::wcout; 11 using std::endl; 12 using std::string; 13 using std::wstring; 14 using std::locale; 15 16 #pragma warning(disable : 4996) 17 18 int main(void) 19 { 20 char buf[] = "我是韩长鸣haizeiwanghancm"; 21 wchar_t wbuf[100]; 22 23 //1. CRT的swprintf()函数 24 MessageBoxW(NULL, L"swprintf", nullptr, 0); 25 //C方式的必须设置本地化locale 26 //locale old_loc = locale::global(locale("")); 27 setlocale(LC_CTYPE, ""); 28 swprintf(wbuf, L"%S", buf); 29 MessageBoxW(NULL, wbuf, NULL, 0); 30 //locale::global(old_loc); 31 32 //2. windows的wsprintf()函数 33 //汉字:wsprintf没问题 34 //%S: 宽字符 35 MessageBoxW(NULL, L"wsprintf", nullptr, 0); 36 wsprintf(wbuf, L"%S", buf); 37 MessageBoxW(NULL, wbuf, NULL, 0); 38 // 也直接使用带A的函数, 39 //在内部它会使用多字节转换函数将字符串传换为wchar_t* 40 MessageBoxA(NULL, buf, NULL, 0); 41 42 43 44 //3. Windows的MultiByteToWideChar(): 都没问题 45 46 MessageBoxW(NULL, L"MultiByteToWideChar函数", nullptr, 0); 47 48 //返回需要的多字节数目(包括结尾'\0') 49 int len = MultiByteToWideChar( 50 CP_ACP, //多字节相关的代码页 51 0, //额外控制,影响变音符号,一般设为0 52 buf, //char缓冲区 53 -1, //char字符个数,-1函数自动判断长度 54 NULL, //wchar_t缓冲区 55 0); //wchar_t缓冲区最大字节数,传入0:不执行转换,返回需要的宽字节数目(包括结尾'\0') 56 57 wchar_t *wbuf2 = new wchar_t[len * sizeof(wchar_t)]; 58 if (wbuf == nullptr) { 59 return 0; 60 } 61 62 MultiByteToWideChar(CP_ACP, 0, buf, -1, wbuf2, len); 63 MessageBoxW(NULL, wbuf2, NULL, 0); 64 delete[] wbuf2; 65 66 //4. CTR的mbstowcs()函数 67 MessageBoxW(NULL, L"mbstowcs函数", nullptr, 0); 68 setlocale(LC_CTYPE, ""); 69 //locale old_loc = locale::global(locale("")); 70 mbstowcs(wbuf, buf, sizeof(buf)); 71 //locale::global(old_loc); 72 MessageBoxW(nullptr, wbuf, nullptr, 0); 73 74 //5. ATL的A2W在MFC中也可以 75 MessageBoxW(nullptr, L"A2W()", nullptr, 0); 76 USES_CONVERSION; 77 MessageBoxW(nullptr, A2W(buf), nullptr, 0); 78 79 //6. ATL的CStringW 80 MessageBoxW(nullptr, L"CString方式", nullptr, 0); 81 CStringW str = buf; 82 MessageBox(nullptr, str, nullptr, 0); 83 84 //7. string转wchar_t* 85 string str1 = "string方式, jsfoej韩长鸣fiok"; 86 wstring ch(str1.size(), 0); 87 setlocale(LC_CTYPE, ""); 88 mbstowcs(&ch[0], str1.c_str(), str1.size()); 89 MessageBoxW(nullptr, ch.c_str(), nullptr, 0); 90 91 //8. CStringA转wchar_t* 92 CStringA CStr = "CStringA方式jsofj韩长鸣eof"; 93 CStringW WStr = CStr; 94 MessageBox(nullptr, WStr, nullptr, 0); 95 96 //9. CStringA转string,直接赋值 97 CStringA Astr(L"韩长鸣sjfo"); 98 string s = Astr; 99 cout << s << endl; 100 101 //10. string转CStringA, CString = string.c_str(); 102 103 //11. CStringA转wstring: CStringW = CStringA; wstring = CStringW; 104 105 //12. string转CStringW: CStringA = string.cstr(); CStringW = CStringA; 106 107 return 0; 108 }
//wchar_t*转char*
#include <cstdio> //for swprintf,sprintf #include <cstdlib> //for mbstowidechar, widechartombs函数 #include <Windows.h> //for wsprintf, MultiByteToWideChar,WideCharToMultiByte #include <atlconv.h> //for A2W(),W2A() #include <atlstr.h> //for CString #include <iostream> //for std::locale #include <string> #include <locale.h> using std::cout; using std::wcout; using std::endl; using std::string; using std::wstring; #pragma warning(disable : 4996) int main(void) { wchar_t wbuf[] = L"我是韩长鸣haizeiwanghancm"; char buf[100]; //1. CRT的sprintf()函数 MessageBoxW(NULL, L"sprintf函数", nullptr, 0); //使用系统locale setlocale(LC_CTYPE, ""); //locale oldlocale = locale::global(locale("")); sprintf(buf, "%S", wbuf); MessageBoxA(NULL, buf, NULL, 0); //locale::global(oldlocale); //2. windows的wsprintfA() //%S: 宽字符 MessageBoxW(NULL, L"wsprintfA函数", nullptr, 0); wsprintfA(buf, "%S", wbuf); MessageBoxA(NULL, buf, NULL, 0); // 也直接使用带W的函数, MessageBoxW(NULL, wbuf, NULL, 0); //3. Windows的WideCharToMultiByte(): 都没问题 MessageBoxW(NULL, L"WideCharToMultiByte()函数", nullptr, 0); //返回需要的字节数目(包括结尾'\0') int len = WideCharToMultiByte( CP_ACP, //多字节相关的代码页 0, //额外控制,影响变音符号,一般设为0 wbuf, //wchar缓冲区 -1, //wchar字符个数,-1函数自动判断长度 NULL, //char缓冲区 0, //char缓冲区最大字节数,传入0:不执行转换,返回需要的宽字节数目(包括结尾'\0') nullptr, //遇到不能转换的字符时的替换字符,nullptr:代表使用系统默认字符,通常? nullptr //表明是否转换成功:TRUE:有不能转换的字符,FALSE:成功 ); char *buf2 = new char[len]; if (wbuf == nullptr) { return 0; } WideCharToMultiByte(CP_ACP, 0, wbuf, -1, buf2, len, nullptr, nullptr); MessageBoxA(NULL, buf2, NULL, 0); delete[] buf2; //4. CTR的mbstowcs()函数 MessageBoxW(NULL, L"wcstombs函数", nullptr, 0); setlocale(LC_CTYPE, ""); //locale old_loc = locale::global(locale("")); wcstombs(buf, wbuf, sizeof(wbuf)); MessageBoxA(nullptr, buf, nullptr, 0); //locale::global(old_loc); //5. ATL的A2W在MFC中也可以 MessageBoxW(nullptr, L"W2A()", nullptr, 0); USES_CONVERSION; MessageBoxA(nullptr, W2A(wbuf), nullptr, 0); //6. ATL的CStringA, CStringA再operator const char* MessageBoxW(nullptr, L"CString方式", nullptr, 0); CStringA str = wbuf; MessageBoxA(nullptr, str, nullptr, 0); //7. wstring转char* wstring str1 = L"wstring方式, jsfoej韩长鸣fiok"; string ch(str1.size(), 0); setlocale(LC_CTYPE, ""); wcstombs(&ch[0], str1.c_str(), str1.size()); MessageBoxA(nullptr, ch.c_str(), nullptr, 0); //8. CStringW转char* CStringW CStr = "CStringW方式jsofj韩长鸣eof"; CStringA AStr = CStr; MessageBoxA(nullptr, AStr, nullptr, 0); //9. CStringW转wstring,直接赋值 CStringW wstr(L"韩长鸣sjfo"); wstring ws = wstr; wcout << ws << endl; //10. wstring转CStringW, CStringW = wstring.c_str(); //11. CStringW转string: CStringA = CStringW; string = CStringA; //12. wstring转CStringA: CStringW = wstring.cstr(); CStringA = CStringW; return 0; }