您的位置:首页 > 编程语言 > C语言/C++

C/C++,Windows/MFC__char与TCHAR相互转化

2015-08-12 20:52 281 查看
char与TCHAR之间的转化主要用到函数MultiByteToWideChar和WideCharToMultiByte

char转TCHAR

如果不是Unicode字符集,就不需要转换,直接复制即可,如果不确定是否使用Unicode字符集,可以这样写

[cpp] view
plaincopy

char strUsr[10] = "Hello";

TCHAR Name[100];

#ifdef UNICODE

MultiByteToWideChar(CP_ACP, 0, strUsr, -1, Name, 100);

#else

strcpy(Name, strUsr);

#endif

TCHAR转char



[cpp] view
plaincopy

char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)

{

LPSTR pszOut = NULL;

if (lpwszStrIn != NULL)

{

int nInputStrLen = wcslen (lpwszStrIn);



// Double NULL Termination

int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;

pszOut = new char [nOutputStrLen];



if (pszOut)

{

memset (pszOut, 0x00, nOutputStrLen);

WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);

}

}

return pszOut;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: