char与TCHAR之间的转化主要用到函数MultiByteToWideChar和WideCharToMultiByte
char转TCHAR
char转TCHAR
如果不是Unicode字符集,就不需要转换,直接复制即可,如果不确定是否使用Unicode字符集,可以这样写
char strUsr[10] = "Hello";
TCHAR Name[100];
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, strUsr, -1, Name, 100);
#else
strcpy(Name, strUsr);
#endifTCHAR转charchar* 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;
}
本文介绍了在C/C++中如何实现char与TCHAR之间的相互转换。针对不同字符集(Unicode与非Unicode),提供了具体的代码示例,包括使用MultiByteToWideChar与WideCharToMultiByte函数的方法。
2847

被折叠的 条评论
为什么被折叠?



