CString char * wchar_t*转换问题
逸萌 2009-01-16 04:35:55 问题在代码描述中
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlstr.h"
using namespace std;
void main()
{
char *orig = "Hello, 中国World!";
cout << orig << " (char *)" << endl;
// Convert to a wchar_t*
size_t origsize = strlen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;//Watch中乱码, 但Console窗口中输出正确
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
#ifndef _UNICODE
cout << cstring << endl;
#else
cout << cstring << endl;//仅输出地址? Watch中查看正确,但在UNICODE时Memory中查看‘中国’的内容d6 d0 b9 fa变成了2d 4e fd 56
wcout << cstring << endl;//仅输出地址?
wcout << cstring.GetBuffer() << endl;//仅输出英语部分?
//TRACE(_T("%s"), cstring)
#endif
// Convert to a basic_string
string basicstring(orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
}