#include <iostream>
#include <string>
#include <windows.h>
#include <tchar.h>
using namespace std;
int main()
{
//1.输出TCHAR类型
TCHAR szbuffer1[] = _T("aaa");
wcout << szbuffer1 << endl;
//2.输出LPTSTR类型
LPTSTR lpbuffer(_T("bbb"));
wcout << lpbuffer << endl;
//3.TCHAR类型转为LPTSTR类型
TCHAR szbuffer2[] = _T("ccc");
LPTSTR lpbuffer2 = szbuffer2;
wcout << lpbuffer2 << endl;
//4.LPTSTR类型转TCHAR类型
TCHAR szbuffer3[100];
LPTSTR lpbuffer3(_T("ddd"));
_tcscpy_s(szbuffer3,100,lpbuffer3);
wcout << szbuffer3 << endl;
//5.TCHAR类型转wstring类型
TCHAR szbuffer4[] = _T("eee");
wstring wstr1(szbuffer4);
wcout << wstr1 << endl;
//6.wstring类型转TCHAR类型
wstring wstr2 =_T("fff");
TCHAR szbuffer5[100];
_tcscpy_s(szbuffer5,100,wstr2.c_str());
wcout << szbuffer5 << endl;
//7.wstring转string
wstring wstr3 = _T("ggg");
string str1(wstr3.begin(),wstr3.end());//也可以调用 assign函数
cout << str1 << endl;
//8.string转wstring
string str2 = "hhh";
wstring wstr4(str2.begin(),str2.end());
wcout << wstr4 << endl;
}
注意 上面的转换不涉及中文
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void ANSIToUnicode(PSTR pMultiByteStr )
{
int len = ::MultiByteToWideChar(CP_ACP,0,pMultiByteStr,-1,NULL,0);
PWSTR c = (PWSTR)HeapAlloc(GetProcessHeap(),0,len* sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0,
pMultiByteStr,
len,
c,
len);
wcout << c << endl;
HeapFree(GetProcessHeap(),0,c);
}
void UnicodeToAnsi(PWSTR pMultiByteStr)
{
int len = ::WideCharToMultiByte(CP_ACP,0,pMultiByteStr,-1,NULL,0,NULL,NULL);
PSTR c = (PSTR)HeapAlloc(GetProcessHeap(),0,len);
::WideCharToMultiByte( CP_ACP,
0,
pMultiByteStr,
len,
c,
len,NULL,NULL);
cout << c << endl;
HeapFree(GetProcessHeap(),0,c);
}
int main()
{
PSTR c = "abc";
ANSIToUnicode(c);
PWSTR s = L"dsf";
UnicodeToAnsi(s);
return 0;
}
注意:windows核心编程 第五版 27页 (3)是错的 不需要乘以sizeof(wchar_t)