1.采用类方式
class a2w
{
wchar_t* buffer;
public:
explicit a2w(const char* str):buffer(0)
{
if(str) {
size_t nu = strlen(str);
size_t n = ::MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),NULL,0);
buffer = new wchar_t[n+1];
::MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),buffer,int(n));
}
}
~a2w() { delete[] buffer; }
operator const wchar_t*() { return buffer; }
};
备注:
explicit:用来修饰类的构造函数,表明该构造函数是显式的。主要防止这种类构造时被编译器自动转换操作。
2.采用函数模式
/*
wBuf 为申明指针即可。
*/
chr2wch(const char* buffer, wchar_t* wBuf)
{
size_t len = strlen(buffer);
size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), NULL, 0);
wBuf = new wchar_t[wlen + 1];
MultiByteToWideChar(CP_ACP, 0, (const_char*)buffer, int(len), wBuf, int(wlen));
}