C/C++,Windows/MFC__char与TCHAR相互转化
2015-08-12 20:52
357 查看
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;
}
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;
}
相关文章推荐
- c语言复习
- Windows,C++中各种数据类型的转换---很有用!
- vc++6对windows SEH扩展分析 一文拾遗
- Windows,C/C++_字符串转换_MultiByteToWideChar和WideCharToMultiByte的正确使用方法及参数详解
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- Android之——JNI与C语言常见术语
- C语言之——文件操作模式
- c++引用总结
- C语言基础C02数组
- C/C++中的const与define
- 如何求C语言字符串长度(strlen函数和sizeof关键字)
- poj1325最小顶点覆盖
- [转载] C++11中的右值引用
- C++ 预处理、const与sizeof 知识点 小结
- Java和C++的主要区别以及Java垃圾回收机制
- C++笔试总结-面试笔试常考题型(二)
- C++怎样跳出双层for循环
- 【C语言经典实例】-使用指针查找数列中的最值
- C++ 控制台版 2048小游戏
- C++ explicit关键字 详解(用于构造函数)