#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
//Converting a WChar string to a Ansi string
string wcs2mbs(const wchar_t *pwcsSrc) {
int nLen = WideCharToMultiByte(CP_ACP, 0, pwcsSrc, -1, 0, 0, 0, 0);
if(nLen <= 0) {
return string("");
}
char *pmbsDst = new char[nLen]();
if(0 == pmbsDst) {
return string("");
}
WideCharToMultiByte(CP_ACP, 0, pwcsSrc, -1, pmbsDst, nLen, 0, 0);
pmbsDst[nLen - 1] = 0;
string strTemp(pmbsDst);
delete [] pmbsDst;
return strTemp;
}
//Converting a Ansi string to WChar string
wstring mbs2wcs(const char *pmbsSrc) {
int nLen = MultiByteToWideChar(CP_ACP, 0, pmbsSrc, -1, 0, 0);
if(nLen <= 0) {
return wstring(L"");
}
wchar_t *pwcsDst = new wchar_t[nLen]();
if(0 == pwcsDst) {
return wstring(L"");
}
MultiByteToWideChar(CP_ACP, 0, pmbsSrc, -1, pwcsDst, nLen);
pwcsDst[nLen - 1] = 0;
wstring wcharString(pwcsDst);
delete pwcsDst;
return wcharString;
}
int main(int argc, char *argv[]) {
string mymbs(wcs2mbs(L"chrisdar的程式設計筆記"));
cout << mymbs << endl;
wstring mywcs(mbs2wcs("chrisdar的程式設計筆記"));
wcout.imbue(locale("cht"));
wcout << mywcs << endl;
wcout.imbue(locale());
system("pause");
return EXIT_SUCCESS;
}
2009年1月12日 星期一
利用 WideCharToMultiByte MultiByteToWideChar 轉換 string wstring
利用 WideCharToMultiByte MultiByteToWideChar 轉換 string wstring
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言